NodeJS/ExpressJS 没有重定向到主页
NodeJS/ExpressJS doesn't redirect to the homepage
我正在 nodejs/expressjs 开发购物车应用程序。在注册时提交用户凭据后,我在重定向回“/”页面时遇到了一些问题。我没有找到与此问题相关的任何文章。我也在使用 CSURF 保护。
问题是如果我按下提交按钮,from
localhost:3000/user/signup/
去
localhost:3000/user/signup?email=something&password=somepassword&_csrf=QQdMG3kT-kOPAucSUipAlAUQaRSoLJrWlMQc
而且它不会返回主页。 (本地主机:3000)
这是 index.js 文件中的代码:
var express = require('express');
var router = express.Router();
var Product = require('../models/product');
var csrf = require('csurf');
var csrfProtection = csrf();
/* GET home page. */
router.use(csrfProtection);
router.get('/', function(req, res, next) {
res.setHeader("Content-Type", "text/html");
Product.find(function(err, docs){
var productChunks = [];
chunkSize = 2;
for(var i = 0; i < docs.length; i+= chunkSize){
productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Humble', products: docs });
});
});
router.get('/user/signup', function(req, res, next){
res.render('user/signup', {csrfToken: req.csrfToken()});
});
router.post('/user/signup', function(req, res, next){
res.redirect('/');
});
module.exports = router;
这是 signup.hbs 文件中的代码:
<h1 class = "subtitlu"><b>Sign UP!</b></h1>
Validation Errors
<form action = "/user/signup" metod = "post"
style =
"box-shadow: 5px 10px 18px #888888;
font-family:Helvetica-Medium;
font-size:25px;
padding:25px;
margin-bottom:30px;
margin-top:30px;
overflow:hidden;">
<div class="form-group">
<label for="email">E-mail</label>
<input type="text" id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Parola</label>
<input type="password" id="password" name="password" class = "form-control">
</div>
<input type = "hidden" name = "_csrf" value = {{ csrfToken }}>
<button type = "submit" class = "btn btn-primary" style="background-color:rgb(0, 219, 66); position: relative;
float: right;">Create an account!</button>
</form>
您的表单中有错字:metod = "post"
将 metod
更新为 method
应该可以完成工作
重定向无效,因为没有 POST 请求到 /user/signup
我正在 nodejs/expressjs 开发购物车应用程序。在注册时提交用户凭据后,我在重定向回“/”页面时遇到了一些问题。我没有找到与此问题相关的任何文章。我也在使用 CSURF 保护。 问题是如果我按下提交按钮,from
localhost:3000/user/signup/
去
localhost:3000/user/signup?email=something&password=somepassword&_csrf=QQdMG3kT-kOPAucSUipAlAUQaRSoLJrWlMQc
而且它不会返回主页。 (本地主机:3000)
这是 index.js 文件中的代码:
var express = require('express');
var router = express.Router();
var Product = require('../models/product');
var csrf = require('csurf');
var csrfProtection = csrf();
/* GET home page. */
router.use(csrfProtection);
router.get('/', function(req, res, next) {
res.setHeader("Content-Type", "text/html");
Product.find(function(err, docs){
var productChunks = [];
chunkSize = 2;
for(var i = 0; i < docs.length; i+= chunkSize){
productChunks.push(docs.slice(i, i + chunkSize));
}
res.render('shop/index', { title: 'Humble', products: docs });
});
});
router.get('/user/signup', function(req, res, next){
res.render('user/signup', {csrfToken: req.csrfToken()});
});
router.post('/user/signup', function(req, res, next){
res.redirect('/');
});
module.exports = router;
这是 signup.hbs 文件中的代码:
<h1 class = "subtitlu"><b>Sign UP!</b></h1>
Validation Errors
<form action = "/user/signup" metod = "post"
style =
"box-shadow: 5px 10px 18px #888888;
font-family:Helvetica-Medium;
font-size:25px;
padding:25px;
margin-bottom:30px;
margin-top:30px;
overflow:hidden;">
<div class="form-group">
<label for="email">E-mail</label>
<input type="text" id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Parola</label>
<input type="password" id="password" name="password" class = "form-control">
</div>
<input type = "hidden" name = "_csrf" value = {{ csrfToken }}>
<button type = "submit" class = "btn btn-primary" style="background-color:rgb(0, 219, 66); position: relative;
float: right;">Create an account!</button>
</form>
您的表单中有错字:metod = "post"
将 metod
更新为 method
应该可以完成工作
重定向无效,因为没有 POST 请求到 /user/signup