简体   繁体   English

Node.js + Express Sessions:如何跟踪上次访问的日期和时间?

[英]Node.js + Express Sessions: How to keep track of date and time of last visit?

I was wondering how one would be able to keep track of a user's last visit on a site with Express? 我想知道如何通过Express跟踪用户对网站的最后访问? For example: 例如:

1.User visits site for the first time, receives a welcome. 1.用户首次访问网站,受到欢迎。

2.The next time user visits site, it will display the date and time of their last visit. 2.下一次用户访问网站时,将显示上次访问的日期和时间。

I've looked into the Express sessions documentation but I didn't see a solution for dealing with this. 我查看了Express会话文档,但没有找到解决此问题的解决方案。

Any insight would be appreciated. 任何见识将不胜感激。

You can use Express sessions combined with the cookies to get this effect. 您可以将Express会话与cookie结合使用以达到这种效果。

 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. 最好的办法是利用HTTP cookie来了解会话的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM