【问题标题】:NodeJS/ExpressJS doesn't redirect to the homepageNodeJS/ExpressJS 不重定向到主页
【发布时间】:2020-06-27 03:13:50
【问题描述】:

我正在 nodejs/expressjs 中开发购物车应用程序。在注册时提交用户凭据后,我在重定向回“/”页面时遇到了一些问题。我没有找到任何与此问题相关的文章。我也在使用 CSURF 保护。 问题是如果我按下提交按钮,来自

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>

【问题讨论】:

  • 你真的不应该在这样的 URL 中输入密码。这会将密码暴露给各种基础设施日志记录。

标签: javascript node.js express redirect express-handlebars


【解决方案1】:

您的表单中有错字:metod = "post"metod 更新为 method 应该可以解决问题

重定向不起作用,因为没有对 /user/signup 的 POST 请求

【讨论】:

  • 天哪,太感谢你了,你让我免于调试 2 小时!
  • 请将此标记为已回答问题已回答!
猜你喜欢
  • 2016-12-20
  • 2013-07-31
  • 2018-01-14
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
相关资源
最近更新 更多