node.js - 表单保存 POST 操作未路由到 Express 中的路由器

标签 node.js express pug

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

当我提交表单时,它没有重定向到 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);
    })

如何从表单调用 post.js 内的 post 路由?

最佳答案

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

关于node.js - 表单保存 POST 操作未路由到 Express 中的路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49667579/

相关文章:

node.js - Express-jwt 中间件 typescript 类型问题

css - 需要按钮 float 到卡片 pug/css 的底部

javascript - 有什么方法可以在node.js 的promise 中抛出错误吗?

node.js - 使用自定义语言的 SSML 输出

css - node.js 不会在所有页面加载 css 样式

pug - 从命令行将变量传递给 Jade 模板

node.js - 如何在 Node.js Express 应用程序中呈现预编译的 Jade (Pug) 模板?

node.js - 如何仅在文本框的图标上执行 vue.js 鼠标悬停事件

javascript - 如何将数据从 Angular 服务发布到 Node ?

Node.js 和 express - 如何读取 cookie 但未定义