Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to use maxAge with Express.js?

I have seen several variations. Suppose I wish my cookie to expire after one second. Should I use

app.use(express.session({ secret: 'mysecret',  maxAge: new Date(Date.now() + 1000)}));

or

app.use(express.session({ secret: 'mysecret',  maxAge: 1000}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: new Date(Date.now() + 1000)}}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: 1000}}));

Also suppose I have set my cookie expiring correctly and it has expired. If the user does not restart their browser do they still retain the cookie information until they do so?

like image 877
Hoa Avatar asked May 03 '12 10:05

Hoa


People also ask

Is Express used for front end?

Express. js is a JavaScript back-end framework that's designed to develop complete web applications and APIs. Express is the back-end component of the MEAN stack, which also includes MongoDB for the database, AngularJS for the front end and Node. js for the JavaScript runtime environment.

What is next () in express JS?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware. Middleware functions can perform the following tasks: Execute any code. Make changes to the request and the response objects. End the request-response cycle.

What does require (' Express ') do?

require('express') returns a function reference. that function is called with express() . app is an object returned by express().

Do people still use express JS?

Express is currently, and for many years, the de-facto library in the Node. js ecosystem. When you are looking for any tutorial to learn Node, Express is presented and taught to people.


1 Answers

I think you'll be better off looking at the source directly.

For Express it uses connect middleware, and here's the code for session https://github.com/senchalabs/connect/blob/master/lib/middleware/session.js#L67

And theres more documentation at the connect site http://www.senchalabs.org/connect/session.html

So you can do

app.use(express.session({ secret: 'mysecret',  cookie: {expires: new Date(Date.now() + 1000)}}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: 1000}}));
like image 98
250R Avatar answered Oct 21 '22 17:10

250R