我想记住有人访问的最后一页(like here)
我尝试用cookie会话来做这件事,但它不能像我想象的那样工作。
我看到了this,我尝试this example来延长会话但没有成功。
这里是代码:
var session = require('cookie-session');
var express = require('express');
var app = express();
app.use( session({ secret: 'secret' }) );
app.get('/a', function (req, res) {
if(req.session.last) {
res.write("the last page was " + req.session.last + ". ");
}
req.session.last = "a";
res.end("Page A");
});
app.get('/b', function (req, res) {
if(req.session.last) {
res.write("the last page was " + req.session.last + ". ");
}
req.session.last = "b";
res.end("Page B");
});
app.get('/c', function (req, res) {
if(req.session.last) {
res.write("the last page was " + req.session.last + ". ");
}
req.session.last = "c";
res.end("Page C");
});
app.listen(8080);
答案 0 :(得分:0)
你确定你的日志中没有出现“在发送后无法写入标题”这一行的错误吗?您可以尝试将会话分配移动到res.write调用之前吗?从您的第一个链接,在评论中......
“我一直收到一个错误,表示在发送之后无法设置标头。 我修改了代码,以便在发送任何正文之前设置lastPage,例如:
var responseText;
if(req.session.lastPage)
responseText = 'Last page was: ' + req.session.lastPage + '. ';
else
responseText = 'You\'re Awesome';
req.session.lastPage = '/awesome';
res.send(responseText);