O'Reilly logo

Node Cookbook Second Edition by David Mark Clements

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Initializing and using a session

If we want to maintain the state between page requests, we use sessions. Express supplies middleware that takes much of the complexity out of managing sessions. In this recipe, we're going to use Express to make a session between a browser and server to facilitate a user login process.

Getting ready

Let's create a fresh project:

express login

This will create a new Express skeleton named login.

Let's move into the login folder and run the following commands:

npm install express-session@1.x --save
npm install method-override @1.x --save

These are both pieces of Express middleware, which we'll use in our new login app.

How to do it...

We need to add some lines to app.js. First, where we require the dependencies, let's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required