express框架中怎么读取post请求发送来的数据给出具体代码
在express框架中,可以使用req.body来读取post请求发送来的数据。
具体代码如下:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// 使用body-parser中间件解析post请求的数据
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 处理post请求
app.post('/post', (req, res) => {
const data = req.body;
console.log(data); // 输出post请求发送来的数据
res.send('Post request received.');
});
// 启动服务
app.listen(3000, () => {
console.log('Server started on port 3000');
});
在上述代码中,使用了body-parser中间件来解析post请求发送来的数据。然后在处理post请求的路由中,通过req.body读取数据,并输出到控制台上。最后,使用res.send响应请求。
原文地址: https://www.cveoy.top/t/topic/bd2F 著作权归作者所有。请勿转载和采集!