我尝试了两种中间件,但仍在终端中获得了此输出:
{}
我的节点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表单中的操作正在起作用,因为我可以在浏览器中看到“这是测试路径”输出
尝试一下
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>
实际上它的工作原理与
app.use(express.urlencoded());
错误的是HTML形式的无名输入