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 require express-session
and method-override
:
var express = require('express'); var http = require('http'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser...