Nit*_*dhe 7 sessionid node.js npm express-session
我面临的问题是:每次请求都会创建新的 sessionID。有人可以阐明这一点吗?
我正在使用的版本:
节点版本:v6.10.3 NPM 版本:3.10.10express@4.15.3express-session@1.15.3
另外,如果我没有专门设置 cookie,那么 maxAge 的默认值是多少?不确定是否需要。
您发现下面的代码有什么问题吗?请协助,因为我被困住了。
var app = express();
app.use(express.static(path.join(__dirname, 'landing')));
app.use(bodyparser.json());
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {secure: true}
}))
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'landing', 'index.html'));
});
var contextPath = '/myportal';
//UI url
app.get(contextPath, function (req, res) {
var reqValues;
logger.info("req sessionID is :::" + req.sessionID);
// do something
}
app.use(express.static(path.join(__dirname, 'build'))); //react UI path
res.sendFile(path.join(__dirname, 'build', 'index.html'));
}
//health check url
app.get(contextPath + '/health', function (req, res) {
logger.info("env is " + env);
logger.info("myportal health is ok");
res.send('Health Ok!\n');
});
Run Code Online (Sandbox Code Playgroud)
如果还有人在寻找答案。(经过大量工作后,以下代码对我有用。如果我错了,请纠正我)
原因:express session 将 sessionID 存储在 cookie 中,并且它会从后端(服务器)在前端(浏览器,您可以在浏览器中看到名为connect.sid的 cookie)设置该 cookie 。每当任何请求首先来自浏览器时,它都会检查该 cookie(其中存储了 sessionID)。如果找到该 cookie,则不会创建新会话,否则它将再次创建一个新会话。(您可以通过记录 req 来检查它)请求中的.sessionID)
解决方案:为了解决我们从前端(浏览器)发出的每个请求的问题,我们必须将该 cookie 发送到后端(服务器)。服务器将自动解析 cookie,并且不会为每个请求创建任何新会话。
我使用 axios 进行请求调用,其中对于每个请求,我添加{withCredentals:true}以便浏览器可以将 cookie 发送到后端服务器(自动)。以下代码对我有用。
应用程序.js
require('dotenv').config({path:'./config.env'});
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors=require('cors');
const uuid = require('uuid/v4')
const cookieParser = require('cookie-parser');
const session = require('express-session');
var FileStore = require('session-file-store')(session);
app.use(cors({
origin:[process.env.ORIGIN],//frontend server localhost:8080
methods:['GET','POST','PUT','DELETE'],
credentials: true // enable set cookie
}));
app.use(cookieParser(process.env.SESSIONSECRET)); // any string ex: 'keyboard cat'
app.use(session({
secret: process.env.SESSIONSECRET,
store:new FileStore,
cookie:{
maxAge:36000,
httpOnly:false,
secure:false // for normal http connection if https is there we have to set it to true
},
resave: false,
saveUninitialized: true
}))
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header("Access-Control-Allow-Origin", process.env.ORIGIN);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content- Type, Accept, Authorization");
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// rest of code is your mongo connection
Run Code Online (Sandbox Code Playgroud)
axios 休息调用::
axios.defaults.withCredentials = true;
axios.get('http://localhost:8080/getDetails',{
headers:{
withCredentials:true,
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11236 次 |
| 最近记录: |