表单保存POST操作不在Express中路由到路由器

时间:2018-04-05 08:26:18

标签: node.js express pug

我加载了一个带端点的表单: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 route?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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