node.js - 快递服务器无法接受 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
解决方案
您缺少 post 函数的第一个参数,它应该是:
app.post("/", (req, res) => {
//handle req.body
res.render("test", data);
})
推荐阅读
- nexmo - 从应用程序到电话(网络)、应用程序的呼叫。多链接号码
- ios - 从 Swift 上的 Watson Customer Engagement 获取渠道和用户 ID
- opengl - OpenGL 纹理包装
- angular - 如何使用 ng add 支持 Angular 6 库
- reactjs - 我怎么知道在 React 中触发了什么渲染?
- sql - 如何过滤掉 SQL 中两个列表的任意组合?
- android - Android App Map Fragment 未在发布版本中显示
- javascript - p5.js createCanvas 不再呈现。ngOnInit const 未被访问
- hbase - 有人可以从“HBase:权威指南”一书中解释这个图吗?
- c# - SQLite 真正的所有字符串行