在文件中使用快速会话和存储会话.但是,每次创建新会话ID和新文件的请求都会被创建. 这是我用来创建会话的代码. app.use(session({ secret: 'keyboard cat', resave: false,store: new FSStore(options), sav
这是我用来创建会话的代码.
app.use(session({ secret: 'keyboard cat', resave: false,store: new FSStore(options), saveUninitialized: false , cookie: { maxAge: 1000,secure: false,httpOnly: true } }))
但是,我希望它应该为每个用户创建一个会话ID或直到会话到期.
我的问题是MaxAge,我将其设置为1000毫秒,即1秒.因此,它每次都在创建一个新的会话ID.我把它设置为1小时,它工作正常.这是完整的代码
var express = require('express'); var app = express(); var session = require('express-session'); var FileStore = require('session-file-store')(session); app.use(session({ secret: 'keyboard cat', resave: false, store: new FileStore, saveUninitialized: false , cookie: { maxAge: 3600000,secure: false, httpOnly: true } }) ); app.get('/', function (req, res) { if (req.session.views) { req.session.views++; res.setHeader('Content-Type', 'text/html'); res.write('<p>views: ' + req.session.views + '</p>'); res.end(); } else { req.session.views = 1; res.end('Welcome to the file session demo. Refresh page!'); } }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });