Expiration object in express-session not the correct type?

老子叫甜甜 提交于 2019-12-25 07:46:27

问题


Every so often, my nodejs application — which uses the express v4.12.2 and express-session v1.13.0 modules — throws the following TypeError exception and crashes:

/app/node_modules/express-session/node_modules/cookie/index.js:136
  if (opt.expires) pairs.push('Expires=' + opt.expires.toUTCString());
                                                       ^

TypeError: opt.expires.toUTCString is not a function
    at Object.serialize (/app/node_modules/express-session/node_modules/cookie/index.js:136:56)
    at setcookie (/app/node_modules/express-session/index.js:576:21)
    at ServerResponse.<anonymous> (/app/node_modules/express-session/index.js:204:7)
    at ServerResponse.writeHead (/app/node_modules/on-headers/index.js:46:16)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.res.write (/app/node_modules/compression/index.js:82:14)
    at ReadStream.ondata (_stream_readable.js:529:20)
    at emitOne (events.js:90:13)
    at ReadStream.emit (events.js:182:7)
    at readableAddChunk (_stream_readable.js:147:16)
    at ReadStream.Readable.push (_stream_readable.js:111:10)
    at onread (fs.js:1822:12)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:614:17)

I'm not sure why this would be an error, since toUTCString() is a function. (Unless opt.expires is not a Date object.)

From testing the application, and also because this seems to involve opt.expires, I am wondering if this happens when a session times out.

Here is how I am setting up sessions:

var express = require('express');
var expressSession = require('express-session');
...
var app = express();
...
app.use(expressSession({
    key: 'application.sid',
    secret: 'some.secret.string',
    cookie: {
        maxAge: 60 * 60 * 1000,
        expires: 60 * 60 * 1000
    },
    store: new mongoStore({
        mongooseConnection: mongoose.connection,
        collection: 'sessions'
    }),
    saveUninitialized: true,
    rolling: true,
    resave: true,
    secure: true
}));

My goal is to have a session expiration get extended if the user keeps using the application.

Is there something wrong about how I have set this up, or have I run into some bug that would be fixed with a specific combination of versions of modules?


回答1:


req.session.cookie.expires must be a date not a number.

Each session has a unique cookie object accompany it. This allows you to alter the session cookie per visitor:

var hour = 3600000;
req.session.cookie.expires = new Date(Date.now() + hour);
req.session.cookie.maxAge = hour;


来源:https://stackoverflow.com/questions/36607293/expiration-object-in-express-session-not-the-correct-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!