如何重定向到 node.js 中的另一个页面
How to redirect to another page in node.js
我有一个登录页面和一个注册页面。当随机用户想要登录并且登录成功时,我想将他重定向到另一个 .ejs 页面(例如 UserHomePage.ejs),但是,到目前为止,我尝试过的任何事情都没有奏效。
if (loggedIn)
{
console.log("Success!");
res.redirect('/UserHomePage');
}
else
{
console.log("Error!");
}
我也想知道如何在点击按钮时重定向用户。
假设我在显示用户页面上,我在其中显示我的所有用户,然后是 "add another used button"。我怎么做?如何在点击后将用户重定向到 Register.js 页面?
<h2>List of users</h2>
<ul>
<% uporabniki.forEach(function(user) { %>
<li>
<%= user.attributes.name %>
<%= user.attributes.last name %>
</li>
<% }); %>
</ul>
<h3>Add another user</h3>
<form method="post">
<input type="submit" value="Add user" />
</form>
如果用户成功登录到您的 Node 应用程序,我认为您正在使用 Express,不是吗?那么您可以使用 res.redirect
轻松重定向。喜欢:
app.post('/auth', function(req, res) {
// Your logic and then redirect
res.redirect('/user_profile');
});
你应该return重定向的行
return res.redirect('/UserHomePage');
If else 语句需要包装在 .get 或 .post 中才能重定向。比如
app.post('/login', function(req, res) {
});
或
app.get('/login', function(req, res) {
});
好的,我会尝试使用我的示例来帮助您。首先,您需要知道我正在使用 express 作为我的应用程序目录结构,并在自动创建 app.js 之类的文件方法。我的 login.html 看起来像:
...
<div class="form">
<h2>Login information</h2>
<form action="/login" method = "post">
<input type="text" placeholder="E-Mail" name="email" required/>
<input type="password" placeholder="Password" name="password" required/>
<button>Login</button>
</form>
这里重要的是action="/login"。这是我在 index.js(用于在视图之间导航)中使用的路径,如下所示:
app.post('/login', passport.authenticate('login', {
successRedirect : '/home',
failureRedirect : '/login',
failureFlash : true
}));
app.get('/home', function(request, response) {
response.render('pages/home');
});
这允许我在成功登录后重定向到另一个页面。有一个有用的教程,您可以查看页面之间的重定向:
http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/
要阅读像 <%= user.attributes.name %> 这样的语句,让我们看一个简单的 profile.html 具有以下结构:
<div id = "profile">
<h3>Profilinformationen</h3>
<form>
<fieldset>
<label id = "usernameLabel">Username:</label>
<input type = "text" id="usernameText" value = "<%= user.user.username %>" />
<br>
</fieldset>
</form>
要获取 user 变量的属性,您必须在 routing.js 中初始化一个 user 变量(在我的例子中称为 index.js)。这看起来像
app.get('/profile', auth, function(request, response) {
response.render('pages/profile', {
user : request.user
});
});
我正在为我的对象模型使用猫鼬:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var role = require('./role');
var userSchema = mongoose.Schema({
user : {
username : String,
email : String,
password : String
}
});
如有其他问题,请随时问我...
最好的祝福,
纳扎尔
@Nazar Medeiros - 您的解决方案使用带有 Express 的护照。我没有使用护照,只是使用 express-jwt。我可能做错了什么,但是当用户登录时,令牌需要 return 到客户端。从我目前发现的情况来看,这意味着我们必须使用令牌 return a json,因此无法调用重定向。有什么我想念的吗?
为了解决这个问题,我只是 return 令牌,将其存储在我的 cookie 中,然后发出 ajax GET 请求(使用有效令牌)。当 ajax 调用 returns 时,我用 returned HTML 替换正文的 html。这可能不是正确的方法,但我找不到更好的方法。这是我的 JQuery JavaScript 代码。
function loginUser(){
$.post("/users/login", {
username: $( '#login_input_username' ).val(),
password: $( '#login_input_password' ).val()
}).done(function(res){
document.cookie = "token = " + res.token;
redirectToHome();
})
}
function redirectToHome(){
var settings = {
"async": true,
"crossDomain": true,
"url": "/home",
"type": "GET",
"headers": {
"authorization": "Bearer " + getCookie('token'),
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
$('body').replaceWith(response);
});
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
另一种方式你可以使用window.location.href="your URL"
例如:
res.send('<script>window.location.href="your URL";</script>');
或:
return res.redirect("your url");
我有一个登录页面和一个注册页面。当随机用户想要登录并且登录成功时,我想将他重定向到另一个 .ejs 页面(例如 UserHomePage.ejs),但是,到目前为止,我尝试过的任何事情都没有奏效。
if (loggedIn)
{
console.log("Success!");
res.redirect('/UserHomePage');
}
else
{
console.log("Error!");
}
我也想知道如何在点击按钮时重定向用户。
假设我在显示用户页面上,我在其中显示我的所有用户,然后是 "add another used button"。我怎么做?如何在点击后将用户重定向到 Register.js 页面?
<h2>List of users</h2>
<ul>
<% uporabniki.forEach(function(user) { %>
<li>
<%= user.attributes.name %>
<%= user.attributes.last name %>
</li>
<% }); %>
</ul>
<h3>Add another user</h3>
<form method="post">
<input type="submit" value="Add user" />
</form>
如果用户成功登录到您的 Node 应用程序,我认为您正在使用 Express,不是吗?那么您可以使用 res.redirect
轻松重定向。喜欢:
app.post('/auth', function(req, res) {
// Your logic and then redirect
res.redirect('/user_profile');
});
你应该return重定向的行
return res.redirect('/UserHomePage');
If else 语句需要包装在 .get 或 .post 中才能重定向。比如
app.post('/login', function(req, res) {
});
或
app.get('/login', function(req, res) {
});
好的,我会尝试使用我的示例来帮助您。首先,您需要知道我正在使用 express 作为我的应用程序目录结构,并在自动创建 app.js 之类的文件方法。我的 login.html 看起来像:
...
<div class="form">
<h2>Login information</h2>
<form action="/login" method = "post">
<input type="text" placeholder="E-Mail" name="email" required/>
<input type="password" placeholder="Password" name="password" required/>
<button>Login</button>
</form>
这里重要的是action="/login"。这是我在 index.js(用于在视图之间导航)中使用的路径,如下所示:
app.post('/login', passport.authenticate('login', {
successRedirect : '/home',
failureRedirect : '/login',
failureFlash : true
}));
app.get('/home', function(request, response) {
response.render('pages/home');
});
这允许我在成功登录后重定向到另一个页面。有一个有用的教程,您可以查看页面之间的重定向:
http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/
要阅读像 <%= user.attributes.name %> 这样的语句,让我们看一个简单的 profile.html 具有以下结构:
<div id = "profile">
<h3>Profilinformationen</h3>
<form>
<fieldset>
<label id = "usernameLabel">Username:</label>
<input type = "text" id="usernameText" value = "<%= user.user.username %>" />
<br>
</fieldset>
</form>
要获取 user 变量的属性,您必须在 routing.js 中初始化一个 user 变量(在我的例子中称为 index.js)。这看起来像
app.get('/profile', auth, function(request, response) {
response.render('pages/profile', {
user : request.user
});
});
我正在为我的对象模型使用猫鼬:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var role = require('./role');
var userSchema = mongoose.Schema({
user : {
username : String,
email : String,
password : String
}
});
如有其他问题,请随时问我... 最好的祝福, 纳扎尔
@Nazar Medeiros - 您的解决方案使用带有 Express 的护照。我没有使用护照,只是使用 express-jwt。我可能做错了什么,但是当用户登录时,令牌需要 return 到客户端。从我目前发现的情况来看,这意味着我们必须使用令牌 return a json,因此无法调用重定向。有什么我想念的吗?
为了解决这个问题,我只是 return 令牌,将其存储在我的 cookie 中,然后发出 ajax GET 请求(使用有效令牌)。当 ajax 调用 returns 时,我用 returned HTML 替换正文的 html。这可能不是正确的方法,但我找不到更好的方法。这是我的 JQuery JavaScript 代码。
function loginUser(){
$.post("/users/login", {
username: $( '#login_input_username' ).val(),
password: $( '#login_input_password' ).val()
}).done(function(res){
document.cookie = "token = " + res.token;
redirectToHome();
})
}
function redirectToHome(){
var settings = {
"async": true,
"crossDomain": true,
"url": "/home",
"type": "GET",
"headers": {
"authorization": "Bearer " + getCookie('token'),
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
$('body').replaceWith(response);
});
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
另一种方式你可以使用window.location.href="your URL"
例如:
res.send('<script>window.location.href="your URL";</script>');
或:
return res.redirect("your url");