const http = require('http');
const queryString = require('querystring')
const server = http.createServer((req, res) => {
var bodyStr = '';
req.on('data', (chunk) => {
bodyStr += chunk;
})
req.on('end', () => {
console.log(bodyStr);
const body = queryString.parse(bodyStr);
console.log(body);
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8'
})
if (body.name && body.age) {
res.write('姓名:' + body.name + '\n');
res.write('年龄:' + body.age + '\n');
} else {
res.write('请提供姓名和年龄参数\n');
}
res.end();
})
})
server.listen(3030, () => {
console.log("服务器启动成功,监听3030端口");
})
老师麻烦帮我看一下为什么我的代码里对于body的打印存在一个空对象?
打印信息:
服务器启动成功,监听3030端口
name=333&age=666
[Object: null prototype] { name: '333', age: '666' }
[Object: null prototype] {}