把
http://localhost:3000/api/create
复制到postman中测试,选择POST方法
把
http://localhost:3000:api/list
复制到 postman中测试,选择GET方法
const http = require("http")
const server = http.createServer((req,res)=>{
// 用户想请求的是哪个路径
// 如果是同样的的路径,但是携带了参数,我们可以把路径后面的参数省略,只取出路径
const url = req.url.split("?")[0]
// 获取请求的方法method
const method = req.method
// 返回,响应数据,会渲染到页面
// 这里的url不要忘记加斜线/
if(url === "/api/list" && method==="GET"){
res.end("this is get api")
}
// 如果路径是/api/create 并且方法是POST
else if(url==="/api/create" && method === "POST"){
res.end("this is create api")
}else{
res.end("this is error")
}
})
// 监听端口
server.listen(3000)
// 会渲染到node.js
console.log("请访问http://localhost:3000");