node.js - NodeJS - 全局变量的值没有改变
问题描述
我想实现一个系统,如果用户键入域/加密/文本(文本被加密的页面),则可以在主页(域)中读取文本。我发现问题是,当我更改 URL 以检查主页数据时,由于我再次加载该页面,所以它被重置,但我找不到任何解决此问题的方法。
const Encryption = require('node_triple_des');
const sha1 = require('sha1');
const express = require('express');
const app = express();
const key = "Chiave_scelta";
var coded_key = 'Coded_Key';
var text_crypted = 'NaN';
var text_decrypted = 'NaN';
async function encryptKey() {
coded_key = sha1(key); //si codifica la chiave scelta in SHA1
}
async function encryptAll(msg) {
await encryptKey();
var result = await Encryption.encrypt(coded_key,msg);
return result;
}
async function decryptAll(msg) {
await encryptKey();
var result = await Encryption.decrypt(coded_key, msg);
return result;
}
app.get('/', function (req, res) {
res.send('Generated key: ' + coded_key + '<br>' +
'Last crypted text: ' + text_crypted + '<br>' +
'Last decrypted text: ' + text_decrypted + '<br>'
)
})
app.get('/crypt/:code', async function (req, res) {
crypted_text = await encryptAll(req.params.code); //cripta il messaggio
res.send('Crypted text: ' + crypted_text )
})
app.get('/decrypt/:code', async function (req, res) {
decrypted_text = await decryptAll(req.params.code); //decripta il messaggio
res.send('Decrypted text: ' + decrypted_text )
})
app.listen(3000)
解决方案
我通过导入模块'local-storage'(我之前安装的)来修复。
const ls = require('local-storage');
要设置值,我使用了以下语法:
ls.set(name,value);
为了获取值,我使用了以下语法:
ls.get(name,value);
推荐阅读
- amazon-web-services - 无法在 EMR spark 集群中运行 python 作业
- python - asyncio.loop.time() 可以与 datetime.datetime.now() 相提并论吗?
- php - trying to downloadable excel between two dates from mysql
- tsql - 查找和替换字符串中的数字
- plc - 将 Excel 数据导入 TwinCat3
- node.js - 在截止日期后屏蔽评论 (democracyos)
- c# - 获取超过 20 个字符的用户名
- html - 如何将背景图像添加到 html 部分?
- python - 从线段列表中查找连接的分支
- google-tag-manager - Google 跟踪代码管理器访问数据层对象值