我如何在带有Express的节点js服务器中接收发布数据?

问题描述 投票:0回答:2

我尝试了两种中间件,但仍在终端中获得了此输出:

{}

我的节点js服务器代码:

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

const app = express();

//the middlewares i tried
app.use(express.urlencoded({extended: false}));
app.use(bodyParser());

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/client.html');
});

app.post('/test', (req, res) => {
   res.send('this is a test path');
   console.log(req.body);
});

app.listen(3000, () => {
  console.log('server listening...');
});

我的表单(client.html文件):

<form method="POST" action="/test">
    <input type="text">
    <input type="submit">
</form>

我还尝试与邮递员发送邮寄数据。我知道html表单中的操作正在起作用,因为我可以在浏览器中看到“这是测试路径”输出

node.js express post
2个回答
0
投票

尝试一下

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

const app = express();


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


app.get('/', (req, res) => {
    res.sendFile(__dirname + '/client.html');
});

app.post('/test', (req, res) => {
   res.send('this is a test path');
   console.log(req.body);
});

app.listen(3000, () => {
  console.log('server listening...');
});

也是html形式,在输入标记中添加name属性

<form method="POST" action="/test">
    <input type="text" name="email">
    <input type="submit">
</form>

0
投票

实际上它的工作原理与

app.use(express.urlencoded());

错误的是HTML形式的无名输入

© www.soinside.com 2019 - 2024. All rights reserved.