这里是引用
const http = require(‘http’);
const url = require(‘url’);
http.createServer((req, res) => {
//get请求方式
if (req.method === "GET") {
//获取url地址问号后面的参数,并转化成对象;
req.query = url.parse(req.url, true).query;
//
res.end(JSON.stringify(req.query));
}
//post请求方式
if (req.method === "POST") {
//1.接收post数据
let postData = "";
//2.监听data事件
req.on('data', chunk => {
postData += chunk;
})
//3.监听end事件
req.on('end', err => {
res.end(postData.toString());
})
}
}).listen(3000, () => {
console.log(‘server at 3000’);
})