Reputation: 333
I was wondering how one would be able to keep track of a user's last visit on a site with Express? For example:
1.User visits site for the first time, receives a welcome.
2.The next time user visits site, it will display the date and time of their last visit.
I've looked into the Express sessions documentation but I didn't see a solution for dealing with this.
Any insight would be appreciated.
Upvotes: 1
Views: 1959
Reputation: 23778
You can use Express sessions combined with the cookies to get this effect.
var express = require('express'); var cookieParser = require('cookie-parser'); var session = require('express-session'); var app = express(); app.use(cookieParser()); app.use(session({secret: "Shh, its a secret!"})); app.get('/', function(req, res){ if(req.session.page_views){ req.session.page_views++; res.send("You visited this page " + req.session.page_views + " times"); } else { req.session.page_views = 1; res.send("Welcome to this page for the first time!"); } }); app.listen(3000);
Here is the tutorial.
The best is to understand how the sessions work by exploiting HTTP cookies.
Upvotes: 2