【问题标题】:form save POST action not routing to router in Express表单保存 POST 操作未路由到 Express 中的路由器
【发布时间】:2018-09-14 23:51:01
【问题描述】:

我加载了一个带有端点的表单:http://localhost:3000/posts/newpost -- GET

当我提交表单时,它不会重定向到 posts/save_post,而是重定向到 http://save_post

当我在动作前面添加帖子时,网址是 http://localhost:3000/posts/posts/save_post

form(role='form', enctype='multipart/form-data', action="/save_post",method="post", style='max-width: 250px;')

    //app.js

    const posts = require('./routes/post');
    app.use('/posts', posts);

    posts.js
    //get end point

    router.get('/newpost',function(req,res){
      res.render('new_post');
    });

    // post endpoint

    router.post('/save_post',function(req,res){
     console.log(req.body.paper_name);
    })

如何从 Form 调用 post.js 中的 post 路由?

【问题讨论】:

  • 你试过用save_post代替/save_post吗?
  • 嘿,您的回答解决了这个问题。谢谢

标签: node.js express pug


【解决方案1】:

错误 const posts = require('./routes/post');如果你的文件是 posts.js,它必须是 const posts = require('./routes/posts');

【讨论】:

    【解决方案2】:

    action="/save_post" 更改为action="/posts/save_post"

    【讨论】:

    • 在这种情况下它的路由到posts/posts/save_post
    • 你的 HTML 表单提交按钮 sn-p 是什么?
    • @Hareeshh 您也可以分享您如何将posts 添加到操作中。谢谢
    猜你喜欢
    • 2021-10-14
    • 2020-10-24
    • 1970-01-01
    • 2021-07-28
    • 2018-08-14
    • 2020-11-24
    • 2016-04-11
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多