Express 4 和文件会话存储
Express 4 and file session store
我使用的是 Express 4,我正在尝试使用 session-file-store 保存会话值,这是我在 app.js
:
中使用的配置
var FileStore = require('session-file-store')(expressSession);
app.use(
expressSession({
name: 'connect.sid',
store: new FileStore(),
cookie : { maxAge : 3600000 * 3 },
resave : true,
saveUninitialized : false,
secret : 'njksndf323SDFsffdi32Sfsd34fsbkoqaz231j0'
})
)
当我在浏览器中打开使用会话的路由时,cookie (connect.sid
) 已正确设置。
问题是每当我刷新页面时,都会在会话目录中创建一些新的会话文件,但我认为在设置 cookie 之后,每个请求都应该使用相同的会话文件。
为什么每次请求都会创建新的会话文件?我错过了什么?
更新:
正在创建新的会话文件,因为所有这些调用都是 ajax 请求。
您的 AJAX 客户端需要支持 withCredentials
(应设置为 true
以使 AJAX 请求传递 cookie and/or 授权 headers).
例如jQuery:
$.ajax({
url : URL,
xhrFields : {
withCredentials: true
}
});
请注意,这需要 CORS 支持
我使用的是 Express 4,我正在尝试使用 session-file-store 保存会话值,这是我在 app.js
:
var FileStore = require('session-file-store')(expressSession);
app.use(
expressSession({
name: 'connect.sid',
store: new FileStore(),
cookie : { maxAge : 3600000 * 3 },
resave : true,
saveUninitialized : false,
secret : 'njksndf323SDFsffdi32Sfsd34fsbkoqaz231j0'
})
)
当我在浏览器中打开使用会话的路由时,cookie (connect.sid
) 已正确设置。
问题是每当我刷新页面时,都会在会话目录中创建一些新的会话文件,但我认为在设置 cookie 之后,每个请求都应该使用相同的会话文件。
为什么每次请求都会创建新的会话文件?我错过了什么?
更新:
正在创建新的会话文件,因为所有这些调用都是 ajax 请求。
您的 AJAX 客户端需要支持 withCredentials
(应设置为 true
以使 AJAX 请求传递 cookie and/or 授权 headers).
例如jQuery:
$.ajax({
url : URL,
xhrFields : {
withCredentials: true
}
});
请注意,这需要 CORS 支持