【问题标题】:Nodejs express handlebars post 404 error '/checkout'Nodejs Express 车把发布 404 错误“/checkout”
【发布时间】:2017-11-21 05:57:30
【问题描述】:

我正在使用 Node、Express、handlebars 和 MongoDB 构建一个购物车项目。目前,当我尝试使用 Jquery 提交表单时,我的“/checkout”文件收到 404 错误。最初,我使用 return false 来阻止表单在收到数据之前提交。为了解决这个问题,我也尝试使用 event.preventDefault。在另一个论坛上,有人建议我的 crsf 保护没有正确创建令牌。我处理了路线并添加到我的视图中,最后添加了 hbs 表单。这些尝试似乎都没有解决我的问题。

这是我的要点的链接。 https://gist.github.com/Satellite9/5e4ce3de5c19cee2f355d872b6d7d3c8

这是我的浏览器发送给我的错误。

*错误:在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:60:13 处未找到 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 接下来 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:635:15 接下来 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:260:14) 在 Function.handle (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:174:3) 在路由器 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 接下来 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\app.js:51:5 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 接下来 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在服务静态 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\serve-static\index.js:75:16) 在 Layer.handle [as handle_request] (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:317:13) 在 C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:335:12) 接下来 (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\express\lib\router\index.js:275:10) 在 SessionStrategy.strategy.pass (C:\Users\Leimamo\PhpstormProjects\untitled2\node_modules\passport\lib\middleware\authenticate.js:325:9) *

这是 npm 给我的错误 发布/结帐 404 206.855 ms -5424

感谢您提供的任何见解。

千里

【问题讨论】:

    标签: javascript node.js http-status-code-404 express-handlebars


    【解决方案1】:

    用我的演示编辑,伙计。

    router.get('/checkout', function(req, res, next) {
     //do something
        });
    
    router.post('/checkout', function(req, res, next) {
    // do something: get params with req.body
        });
    

    我使用路线并添加到我的视图中,最后添加到 hbs 表单

    hbs 表单:我在提交表单时使用路由和帖子

    【讨论】:

    • 嗨 Anhlee,我不确定我是否遵循了您的建议。你是说动词也不对吗?感谢您的回复。
    【解决方案2】:

    您在 index.js 上使用了错误的动词。更改get->post。

    router.get('/checkout', function(req, res, next) {
    ......
    });
    

    router.post('/checkout', function(req, res, next) {
    ....
    });
    

    【讨论】:

    • 您好 Fazal,当我将动词从 router.get 更改为 router.post 时,我收到路由“POST /shop/checkout 404”的 404 错误。感谢您的回复。
    • 我尝试了几种不同的方法;将路由更改为 router.post '/shop/checkout',但错误仍然返回了对 '/checkout' 的失败搜索。
    猜你喜欢
    • 2015-12-13
    • 2016-12-06
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多