Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add session support to express after setup

I am just starting to figure out nodejs and I forgot to put in the flag for session support

$ express -s somefolder

Can I run the above command without overwriting anything I already added or changed or do I have to do something else?

it is not as clear as adding a new dependancy (stylus) to package.json and rerun $ npm install

like image 696
Richard Avatar asked May 03 '26 14:05

Richard


1 Answers

Update:

Session support is now added via the expressjs/session module.

To install:

npm install -save express-session

To use:

import * as session from "express-session";
...
app.use(cookieParser());
app.use(session({ secret: "..." });

Be sure to visit the module on GitHub to get the latest installation and usage instructions.

Original answer:

Just add the session middleware to your Express app.js file.

app.use(express.cookieParser());
app.use(express.session({secret: '1234567890QWERTY'}));

Make sure it comes after the express.cookieParser() call. Also, update the secret value to a random string for security.

like image 52
Daniel Avatar answered May 06 '26 04:05

Daniel