In this post we will be discussing about managing sessions in Express. We use sessions if we want to maintain state between page requests. In Express we have middleware which takes care of the session complexity. A session is a way to store information to be used across multiple pages. The information is not stored on the users computer in case of sessions.
How to use Session
At first you need to install express-session in your system. Express Session creates a session middle-ware for your project. Then we need to initialize the session as shown below
app.use(session({secret: 'codingdefined}));
Secret is a required option which is used to sign the session ID cookie. This can be either a string for a single secret, or an array of multiple secrets. If an array of secrets is provided, only the first element will be used to sign the session ID cookie, while all the elements will be considered when verifying the signature in requests.
You might get a warning like express-session deprecated undefined resave option and/or express-session deprecated undefined saveUninitialized option.
To remove this warning you need to initialized your session with two more options (resave and saveUninitialized) as shown below
app.use(session({secret: 'codingdefined', resave: false, saveUninitialized: true}));
Then we will use request object to set or retrieve sessions as shown below where mySession is the variable which you can use to assign session to any variable :
var mySession;
app.get('/', function(req,res) {
mySession = req.session;
res.send('Hello World Express');
});
Remembering Previous Page application in Node.js Using Express Session
var express = require('express');
var session = require('express-session');
var path = require('path');
var app = express();
var port = 5000;
app.set('port', port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(session({secret: 'codingdefined', resave: false, saveUninitialized: true}));
app.get('/', function(req, res) {
req.session.pageVisited = "homepage";
res.send('Hello World Express');
});
app.get('/policy', function(req, res) {
if(req.session.pageVisited) {
res.write('You visited our last page which was ' + req.session.pageVisited);
}
req.session.pageVisited = "policy";
res.end();
});
app.get('/about', function(req, res) {
if(req.session.pageVisited) {
res.write('You visited our last page which was ' + req.session.pageVisited);
}
req.session.pageVisited = "about";
res.end();
});
var server = app.listen(port, function () {
console.log('Listening on port ' + server.address().port)
});
In the above code when you visit the homepage we are setting a session variable called pageVisited as "homepage". Now when you visit "policy" or "about" page, you can use the same session variable to keep track of the previous page visits. At first we need to check that the session variable (pageVisited) is been set or not. If it is set means there is an active session otherwise there is no session. If you don't know about routing, check Basic Routing in Express.
When I visited about page and then policy page the output is something like below.
Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.
How to use Session
At first you need to install express-session in your system. Express Session creates a session middle-ware for your project. Then we need to initialize the session as shown below
app.use(session({secret: 'codingdefined}));
Secret is a required option which is used to sign the session ID cookie. This can be either a string for a single secret, or an array of multiple secrets. If an array of secrets is provided, only the first element will be used to sign the session ID cookie, while all the elements will be considered when verifying the signature in requests.
You might get a warning like express-session deprecated undefined resave option and/or express-session deprecated undefined saveUninitialized option.
To remove this warning you need to initialized your session with two more options (resave and saveUninitialized) as shown below
app.use(session({secret: 'codingdefined', resave: false, saveUninitialized: true}));
Then we will use request object to set or retrieve sessions as shown below where mySession is the variable which you can use to assign session to any variable :
var mySession;
app.get('/', function(req,res) {
mySession = req.session;
res.send('Hello World Express');
});
Remembering Previous Page application in Node.js Using Express Session
var express = require('express');
var session = require('express-session');
var path = require('path');
var app = express();
var port = 5000;
app.set('port', port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(session({secret: 'codingdefined', resave: false, saveUninitialized: true}));
app.get('/', function(req, res) {
req.session.pageVisited = "homepage";
res.send('Hello World Express');
});
app.get('/policy', function(req, res) {
if(req.session.pageVisited) {
res.write('You visited our last page which was ' + req.session.pageVisited);
}
req.session.pageVisited = "policy";
res.end();
});
app.get('/about', function(req, res) {
if(req.session.pageVisited) {
res.write('You visited our last page which was ' + req.session.pageVisited);
}
req.session.pageVisited = "about";
res.end();
});
var server = app.listen(port, function () {
console.log('Listening on port ' + server.address().port)
});
In the above code when you visit the homepage we are setting a session variable called pageVisited as "homepage". Now when you visit "policy" or "about" page, you can use the same session variable to keep track of the previous page visits. At first we need to check that the session variable (pageVisited) is been set or not. If it is set means there is an active session otherwise there is no session. If you don't know about routing, check Basic Routing in Express.
When I visited about page and then policy page the output is something like below.
Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.
No comments:
Post a Comment