Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express会话未使用Expression Session-connect redis存储在redis中_Javascript_Node.js_Express_Redis_Router - Fatal编程技术网

Javascript Express会话未使用Expression Session-connect redis存储在redis中

Javascript Express会话未使用Expression Session-connect redis存储在redis中,javascript,node.js,express,redis,router,Javascript,Node.js,Express,Redis,Router,是否有人在Redis中存储表达式会话的工作代码 Iam正在创建angular 4登录页面。我想使用Express session在Redis中存储用户会话 Iam获取错误会话未定义 如何在Redis中查看sessionID 我被这个打动了。是否有任何机构面临同样的问题?感谢您的帮助您可以通过使用express session和connect redis来实现 一个完整的例子: const express = require('express'); const app = express();

是否有人在Redis中存储表达式会话的工作代码

Iam正在创建angular 4登录页面。我想使用Express session在Redis中存储用户会话

Iam获取错误会话未定义

如何在Redis中查看sessionID


我被这个打动了。是否有任何机构面临同样的问题?感谢您的帮助

您可以通过使用express session和connect redis来实现

一个完整的例子:

const express = require('express');
const app = express();

const session = require('express-session');
const RedisStore = require('connect-redis')(session);

// Create redis client
const redis = require('redis');
// default client tries to get 127.0.0.1:6379
// (a redis instance should be running there)
const client = redis.createClient();
client.on('error', function (err) {
  console.log('could not establish a connection with redis. ' + err);
});
client.on('connect', function (err) {
  console.log('connected to redis successfully');
});

// Initialize middleware passing your client
// you can specify the way you save the sessions here
app.use(session({
  store: new RedisStore({client: client}),
  secret: 'some secret',
  resave: false,
  saveUninitialized: true
}));

app.get('/', (req, res) => {
  // that's how you get the session id from each request
  console.log('session id:', req.session.id)
  // the session will be automatically stored in Redis with the key prefix 'sess:'
  const sessionKey = `sess:${req.session.id}`;
  // let's see what is in there
  client.get(sessionKey, (err, data) => {
    console.log('session data in redis:', data)
  })
  res.status(200).send('OK');
})

app.listen(3000, () => {
  console.log('server running on port 3000')
})

/*
If you check your redis server with redis-cli, you will see that the entries are being added:
$ redis-cli --scan --pattern 'sess:*'
*/
有关更多信息,您可能需要阅读和


希望这有帮助

我在过去的两天里一直在尝试这个,但没有得到任何回报。谢谢你的帮助,我也在使用express session和connect redis。我尝试了从redis获取代码,client.getsessionKey,err,data=>{console.log'session data in redis:',data}。但它返回null。它没有存储在redis中。我不确定会话存储在何处。client.get中的err对象将获得什么输出@JaneError还返回'Null'。您是否能够看到成功连接到redis的控制台日志?您能否从命令行使用redis cli访问本地redis实例?是的,redis正在连接,并且能够从命令行访问redis实例。