【问题标题】:express server cannot accept html form post快递服务器不能接受 html 形式的帖子
【发布时间】:2019-04-07 04:36:45
【问题描述】:

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

哈巴狗代码:

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.js forms pug


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 2010-12-29
      • 2021-02-24
      • 1970-01-01
      • 2010-10-02
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多