所以我有一个名为"登录"的网站的事件处理程序:
function _GetLoginEventHandler(req, res) {
var answer = _getHTMLSkeleton();
var dynamicAnswer;
if (isLogedIn == false) {
dynamicAnswer = "<h1>Login</h1>" + //creates a form for not logged users
"<form action='/login' method='post'>" +
"User name: <br>" +
'<input type="text" name="username" value="MickeyMouse">' +
"<br>" +
"Password: <br>" +
'<input type="password" name="password" value="geheim">' +
"<br><br>" +
'<input type="submit" value="Submit">' +
"</form> ";
answer = answer.replace('{title}', "Login");
answer = answer.replace("{body}", dynamicAnswer);
res.write(answer);
res.end();
}
else {
res.writeHead(302, { //if the user is loged it, it redirects to his "profile"
'Location': '/user'
});
res.end();
}
};
如果用户已登录,则会将其重定向到名为&#34; user&#34;在我的情况下,他的个人资料:
function _GetUserEventHandler(req, res) {
var answer = _getHTMLSkeleton();
var dynamicAnswer;
if (isLogedIn == true) {
dynamicAnswer = "<h1>Logg Off</h1>" +
"<p>Your user id is: " + uID + "</p>" +
"<form action='/user' method='post'>" +
'<input type="submit" value="Submit">' + "</form>";
}
else
dynamicAnswer = "<h4>You are not allowed to see this content, you are not loged in </h4>";
answer = answer.replace('{title}', "User");
answer = answer.replace("{body}", dynamicAnswer);
res.write(answer);
res.end();
}
我想检查主页是否从(在这种情况下)登录页面重定向,以便我可以添加到&#34;回答&#34;例如&#34;您已成功登录&#34;。 PS:_getHTMLSkeleton返回带有占位符的HTML页面的结构
答案 0 :(得分:-1)
您可以检查申请中的引荐来源标头是否为您的登录表单:
req.headers.referer
如果您的应用设置了引荐来源值(请检查原点。
或尝试设置标题:
res.setHeader('fromLoginPage', 'true');
在重定向用户之前,稍后再检查:
req.headers.fromLoginPage == true