直接看代码
const http=require('http')
const server=http.createServer((req,res)=>{
if(req.method==="POST"){
// req的数据格式
console.log('req content-type:',req.headers['content-type'])
//用这个查看数据的格式
// 接收数据
let postData='';
req.on('data',chunk=>{
postData+=chunk.toString()
})
req.on('end',()=>{
console.log('postData: ',postData)
res.end('回应请求');
})
}
})
server.listen(3000,()=>{
console.log('listen on 3000 port');
})
用google postman怎么发送请求
先node 运行上面程序
打开之后将图片所指的地方改为post,然后输入监听的本地ip+端口号(http://127.0.0.1:3000)如下图:
然后依次点击body->row-> JSON(application/json),在下面随便填入一组json数据然后点击发送,如下图
send之后