Node.js/Express post路由失败

Node.js/Express post路由失败,node.js,express,Node.js,Express,第一次尝试Node.js。我的发布路径因“无法发布/admin/add product.html”而失败。GET请求正从同一路线得到良好的服务。我已经在这附近看过了。对类似问题有几个答案,但没有任何帮助。这是我的密码: /index.js const path = require('path'); const express = require('express'); const bodyParser = require('body-parser'); const app = express

第一次尝试Node.js。我的发布路径因“无法发布/admin/add product.html”而失败。GET请求正从同一路线得到良好的服务。我已经在这附近看过了。对类似问题有几个答案,但没有任何帮助。这是我的密码:

/index.js

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

const adminRoutes = require('./src/routes/admin');
const shopRoutes = require('./src/routes/shop');

app.use(bodyParser.urlencoded({ extended: false }));

app.use('/admin', adminRoutes);
app.use(shopRoutes);

app.listen(3000);`
/src/routes/admin.js

const path = require('path');

const express = require('express');

const router = express.Router();

// served on /admin/add-product GET route
router.get('/add-product', (req, res, next) => {
  res.sendFile(path.join(__dirname, '../', 'views', 'add-product.html'));
});

// served on /admin/add-product POST route
router.post('/add-product', (req, res, next) => {
  console.log(req.body);
  res.redirect('/');
  // res.send('<h1>product saved</h1>');
});

module.exports = router;
const path=require('path');
const express=require('express');
const router=express.router();
//服务于/管理/添加产品获取路线
router.get('/add product',(请求、回复、下一步)=>{
res.sendFile(path.join(uu dirname,“../”,“views”,“add product.html”);
});
//服务于/管理/添加产品发布路线
router.post(“/add product”,(请求、回复、下一步)=>{
控制台日志(请求主体);
res.redirect('/');
//res.send(“保存的产品”);
});
module.exports=路由器;
./src/views/add-product.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Add Product</title>
  </head>
  <body>
    <header>
      <nav>
        <ul>
          <li><a href="/">Shop</a></li>
          <li><a href="add-product">Add Product</a></li>
        </ul>
      </nav>
    </header>
    <main>
      <form action="/admin/add-product.html" method="POST">
        <input type="text" name="title" /><button type="submit">
          Add Product
        </button>
      </form>
    </main>
  </body>
</html>

添加产品
添加产品

谢谢你

表单中的操作需要与NodeJS应用程序中的路由声明匹配,它应该是:

<form action="/admin/add-product" method="POST">


从表单的action属性中删除.HTML。

您可以尝试添加到注释中吗?要添加注释,您的帖子路径没有
.HTML
,因此您必须从表单的action属性中删除
.HTML
。谢谢。它修好了!:)如果您将其作为答案发布,我很高兴将其标记为正确答案。@ImranAli很高兴能提供帮助,我添加了我的答案:)