[英]Nodejs redirection is not working
I am new to node . 我是node的新手。 Whatever I am trying to is a simple from submission and after submission redirect to specific template .
我要尝试的只是从提交到提交后重定向到特定模板的简单方法。 Here is my directory structure
这是我的目录结构
Here is my index.js
file 这是我的
index.js
文件
var app = require('express')();
var http = require('http').Server(app);
var express = require('express')
var bodyParser = require('body-parser')
app.get('/', function(req, res) {
res.sendFile(__dirname + '/login.html');
});
app.post('/login', function(request, response) {
console.log(request.body.user);
console.log(request.body.password);
if (request.body.user === "test@gmail.com" && request.body.password === "123") {
console.log("logged in");
response.statusCode = 200;
response.setHeader("Location", "/home");
response.end();
}
});
app.get('/home', function(req, res) {
res.sendFile(__dirname + '/test.html');
});
http.listen(3001, function() {
console.log('listening on *:3001');
});
In my login.html
file I have a simple form 在我的
login.html
文件中,我有一个简单的表格
<label for="user" class="sr-only">Email address</label>
<input type="email" id="user" class="form-control" name="user[email]" placeholder="Email address" required autofocus autocomplete="on">
<label for="password" class="sr-only">Password</label>
<input type="password" id="password" name="user[password]" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="button" id="submit">Sign in</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
var user, pass;
$("#submit").click(function(event) {
user = $("#user").val();
pass = $("#password").val();
$.post("/login", {user: user, password: pass}, function(data) {
if (data === 'done')
{
alert("login success");
}
});
});
});
</script>
When I run my app at localhost:3001
login.html
appears , if I submit the form with username:test@gamil.com
and password:123
it doesn't redirect to test.html
, though I can read the console message "logged in"
. 当我在
localhost:3001
运行我的应用程序时,出现login.html
,如果我使用username:test@gamil.com
和password:123
提交表单,则它不会重定向到test.html
,尽管我可以阅读控制台消息"logged in"
。
Instead of making the request to /login
through an AJAX call ( $.post()
), just let the form submit to it instead: 而不是通过AJAX调用(
$.post()
)来请求/login
的请求,只需让表单提交给它:
<form action='/login' method='post'>
...
</form>
In your current situation, you're redirecting the AJAX request to /home
, not your entire page. 在当前情况下,您要将AJAX请求重定向到
/home
,而不是整个页面。
Also, your handler does nothing when the username and/or password don't match, which means that the request will stall. 同样,当用户名和/或密码不匹配时,您的处理程序也不执行任何操作,这意味着请求将停止。 You need to send a response back in that case as well (perhaps redirect to an error page, or to the login page again).
在这种情况下,您还需要发送回响应(也许重定向到错误页面,或再次重定向到登录页面)。
Could you try using 你可以尝试使用
app.post('/login', function(request, response) {
console.log(request.body.user);
console.log(request.body.password);
if (request.body.user === "test@gmail.com" && request.body.password === "123") {
console.log("logged in");
response.redirect('/home')
}
});
Documentation on express redirect here( http://expressjs.com/en/api.html#res.redirect ) 有关快速重定向的文档,请点击此处( http://expressjs.com/en/api.html#res.redirect )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.