最简单的http服务
//创建http服务
const http = require('http')//创建http模块引用
const server = http.createServer()//创建服务
server.listen(8808)//监听端口
server.on('request',(request,response)=>{//每次被调用,执行方法
response.statusCode = 200;
response.end("Welcome to my first server")
})
Node 运行文件
Postman 获取 localhost:8808 端口的请求
const http = require('http')//创建http模块引用
const server = http.createServer()//创建服务
server.listen(8808)//监听端口
server.on('request',(request,response)=>{
console.log(request.url);
response.statusCode = 200;
response.end("Welcome to my first server")
})
/
localhost是127.0.0.1的别名
段号192或172或10 查询内网IP
ifconfig | grep 192
用内网IP替换localhost发送请求,结果一样
Webstorm node 调试
-
Add Configurations
image.png
image.png
image.png - Debuuger
设置断点后,Debug,在 Postman上重新发送请求
解析 query
const http = require('http')//创建http模块引用
const server = http.createServer()//创建服务
server.listen(8808)//监听端口
const qs = require('querystring')
server.on('request', (request, response) => {
const url = request.url
const querySting = url.substr(url.indexOf('?')+1,url.length)
const query = qs.parse(querySting)
console.log(query);
})
网友评论