首页 > 解决方案 > 快递服务器无法接受 html 表单发布

问题描述

每次我尝试向我的服务器发帖时,出于某种原因,浏览器都会向我显示以下消息:“无法发布 /”,我不知道为什么。我会很感激一些见解。

哈巴狗代码:

doctype html
html
        head
                title This is a test
        body
                form(method= "post", enctype="multipart/form-data")
                        input(type="text" placeholder="insert name here" name="username")
                        input(type="file")
                        button(type= "submit") Please submit
                if reqON_Data
                        each submission in reqON_Data
                                ul
                                        li=submission 

节点代码:

  1 const express = require('express');
  2 
  3 const app = express();
  4 
  5 if(!app.locals.newData) app.locals.newData = [];
  6 else console.log("app.locals already exists");
  7 
  8 app.set('views', 'views');
  9 app.set('view engine', 'pug');
 10 
 11 app.get("/", (req, res)=>{ 
 12         res.render("test", { "reqON_Data": app.locals.newData});
 13 });
 14 app.post((req, res)=>{
 15         let formData = '';
 16         req.on('data', (d)=>{
 17                 formData += d;
 18         });     
 19         app.locals.newData.push(formData);
 20         res.redirect('/');
 21 });     
 22 
 23 app.listen(8080);
 24 
 25 

标签: node.jsformspug

解决方案


您缺少 post 函数的第一个参数,它应该是:

app.post("/", (req, res) => {
   //handle req.body
   res.render("test", data);
})

推荐阅读