gpt4 book ai didi

node.js - Express.js session 未定义。雷迪斯

转载 作者:IT王子 更新时间:2023-10-29 06:09:42 24 4
gpt4 key购买 nike

尝试使用 Redis 设置 session 。我的 Redis 数据库在一个 dokku 容器中,链接到我的应用程序(也在一个 dokku 容器中)。我不断得到一个未定义的 session 。我已经将事情剥离到最低限度,还检查了事情的运行顺序。我仍然得到一个未定义的。

我读过这里的 'session' is undefined when using express / redis for session storeExpress js session undefined 无济于事。

我不需要使用 cookie-parser,因为 expression-session 中有 cookie 的东西,文档说 cookie-parser 可能会导致 expression-session 出现问题。

var express = require('express');
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 5000));

var redisURL = 'redis://xxxxx:1234567@bar-redis-foo:6379';
var store = new redisStore({ url: redisURL });

app.use(session({
secret: 'ssshhhhh',
store: store,
saveUninitialized: true,
resave: true
}));


app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));

app.get('/', function(req, res, next) {
console.log(req.session); // Logs Undefined
res.send('Hello');
});

最佳答案

检查您的 redis 连接并再次运行。示例代码如下。

"use strict";

const express = require("express");
const bodyParser = require("body-parser");
const session = require("express-session");
const RedisStore = require("connect-redis")(session);

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use(session({
secret: "$kx(Fj$uB!Ug!@jCkguFmc6f7t<c-e$9",
resave: false,
saveUninitialized: true,
store: new RedisStore({
url: "redis://:********@pub-redis-12766.eu-central-1-1.1.ec2.redislabs.com:12766",
ttl: 5 * 60 // 5 minute (Session store time)
})
}));

app.use(function (request, response, next) {
let path = request.originalUrl;

if (request.session.user) {
request.session.reload(function (err) { //session expire time regenerate
if (!err) {
next();
} else {
response.redirect('/login');
}
});
} else {
if (path == '/login') {
next();
} else {
response.redirect('/login');
}
}
});

app.get('/', function(request, response) {
if (request.session.user) {
response.send(request.session.user);
} else {
response.redirect("/login");
}
});

app.get('/login', function(request, response) {
if (request.session.user) {
response.redirect("/");
} else {
request.session.user = {username: "halil"}; //custom key {user} and custom data {username: "halil"}
}

response.send('Login');
});

app.get('/logout', function(request, response) {
if (request.session.user) {
request.session.destroy();
response.redirect("/login");
} else {
response.redirect("/login");
}
});

app.listen(app.get('port'), function () {
console.log('App is working on port: ' + app.get('port'));
});

关于node.js - Express.js session 未定义。雷迪斯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40456897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com