0
我使用http-proxy-middleware構建代理,併爲每個請求生成新會話,因此我無法使用session.anynoe中的令牌存儲驗證登錄狀態。想法來解決這個問題。http-proxy-middleware爲每個請求創建新會話
我使用http-proxy-middleware構建代理,併爲每個請求生成新會話,因此我無法使用session.anynoe中的令牌存儲驗證登錄狀態。想法來解決這個問題。http-proxy-middleware爲每個請求創建新會話
// proxy middleware options
var options = {
target: 'http://localhost:8081', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
logLevel: "debug",
pathRewrite: {
'^/src/wxmservice/' : '/wxmservice/'
},
onProxyRes: function (proxyRes, req, res) {
if (proxyRes.headers['set-cookie'] != undefined) {
req.session['cookie'] = proxyRes.headers['set-cookie']; // must be or you will get new session for each call
req.session['proxy-cookie'] = proxyRes.headers['set-cookie']; // add to other key because cookie will be lost
}
console.log("response: " + req.session.id);
console.log(req.session);
},
onProxyReq: function (proxyReq, req, res) {
// check for whether the session be freshed
if (req.session.view)
req.session.view ++;
else
req.session.view = 1;
// use ower key to restore cookie
if (req.session['proxy-cookie'] != undefined)
proxyReq.setHeader('cookie', req.session['proxy-cookie'][0]);
console.log("request: " + req.session.id);
console.log(req.session);
}
};
var session = require('express-session');
app.use(session({
secret: 'sessiontest',
resave: true,
saveUninitialized:true
}));