处理get请求
const http = require('http');
const querystring = require('querystring');
const server = http.createServer((req,res) =>{
console.log(req.method) // GET 获取请求方法
const url = req.url
req.query = querystring.parse(url.split('?')[1])
//解析 querystring (get参数)
res.end(JSON.stringify(req.query));
})
server.listen(8000); 监听8000 端口
处理post请求
const http = require('http')
const server = http.createServer(( req,res)=>{
if(req.method === 'POST'){
console.log('content-type',req.headers['content-type'])
let postData = ""
req.on('data', chunk => {
postData += chunk.toString()
})
req.on('end',()=>{
console.log(postData)
res.end('hello world')
})
}
})