首页 > 解决方案 > 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)

标签: node.js

解决方案


我通过导入模块'local-storage'(我之前安装的)来修复。

const ls = require('local-storage');

要设置值,我使用了以下语法:

ls.set(name,value);

为了获取值,我使用了以下语法:

ls.get(name,value);

推荐阅读