快递服务器无法接受 html 形式 post
express server cannot accept html form post
每次我尝试向我的服务器发送 post 时,出于某种原因,浏览器会向我显示此消息:"Cannot POST /",我不确定为什么。我将不胜感激。
PUG 代码:
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
NodeJS 代码:
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);
})
每次我尝试向我的服务器发送 post 时,出于某种原因,浏览器会向我显示此消息:"Cannot POST /",我不确定为什么。我将不胜感激。
PUG 代码:
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
NodeJS 代码:
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);
})