//先引入http const http = require(‘http‘) const querystring=require(‘querystring‘) //url字符串格式化模块
const server = http.createServer((req, res) => { const url=req.url //获取url后面的数据,如http://localhost:8080/api/list,返回/api/list let data = ‘‘ req.on(‘data‘, (chunk) => { data += chunk }) req.on(‘end‘, () => { res.writeHead(200, { ‘content-type‘: ‘application/json;charset=utf-8‘ //发送json格式数据 }) res.write(JSON.stringify(querystring.parse(data))) //将url字符串格式化为string字符串 res.write(`{"url":"${url}"}`) //向前端发送数据 res.end() }) }) server.listen(8080, () => { console.log(‘localhost:8080‘) })