First, introduce the http module
let http=require('http');
Then introduce url: get routing parameter npm i url
let url=require('url');
then create the service
let server=http.createServer(function(req,res){
//从请求中拿到路由参数
let params=url.parse(req.url);
res.statusCode=200; //正确的状态吗
//设置请求头
res.setHeader('Content-Type','application/json;charset=utf-8')
//给前端的响应结果
res.end(`{"errCode":0,"msg":"${
params.search}"}`);
})
Finally start the service
server.listen(8888,function(){
console.log("服务启动成功");
})
Stop the service here with ctrl + c