user.js代码
const handleUserRouter = (req,res)=>{
const method = req.method //GET POST
const url = req.url
const path = url.split(’?’)[0]
//获取博客列表
if(method===‘POST’&& path===’/api/user/login’){
return {
msg:‘这是登录的接口’
}
}
}
module.exports = handleUserRouter
app.js代码
const handleBlogRouter = require(’./src/router/blog.js’)
const handleUserRouter = require(’./src/router/user.js’)
const serverHandle = (req,res)=>{
//设置返回格式JSON
res.setHeader(‘Content-type’,‘application/json’)
const blogData = handleBlogRouter(req,res)
if(blogData){
res.end(
JSON.stringify(blogData)
)
return
}
const userData = handleUserRouter(req,res)
if(userData){
res.end(
stringify(userData)
)
return
}
res.writeHead(404,{“Content-type”:“text/plain”})
res.write(“404 NOT FOUND\n”)
res.end()
}
module.exports = serverHandle
www.js代码
const http = require(‘http’)
const serverHandle = require(’…/app.js’)
const PORT = 8000
const server = http.createServer(serverHandle)
server.listen(PORT)
现在问题是blog都能跑通,唯有这该死的user无论如何折腾要不链接不上,要不404,真是见鬼了,学点东西要这么阻碍我前进!