项目列表
www.js
const http = require('http')
const PORT = 9000
const serverHandle = require('../app.js')
const server = http.createServer(serverHandle)
server.listen(PORT)
console.log('OK')
blog.js
const handleBlogRouter = (req, res) => {
const method = req.method;
if (method === 'GET' && req.path === '/api/blog/list') {
return {
msg: '我是博客列表的接口'
}
}
if (method === 'GET' && req.path === '/api/blog/detail') {
return {
msg: '我是博客详情的接口POST'
}
}
if (method === 'POST' && req.path === '/api/blog/new') {
return {
msg: '我是新建一条博客'
}
}
if (method === 'POST' && req.path === '/api/blog/update') {
return {
msg: '我是更新一条博客'
}
}
if (method === 'POST' && req.path === '/api/blog/del') {
return {
msg: '我是删除一条博客'
}
}
}
module.exports = handleBlogRouter;
user.js
const handleUserRouter = (req, res) => {
const method = req.method;
if (method === 'POST' && req.path === '/api/user/login') {
return {
msg: '我是登陆的接口'
}
}
}
module.exports = handleUserRouter;
app.js
const handleBlogRouter = require('./src/router/blog')
const handleUserRouter = require('./src/router/user')
const serverHandle = (req,res)=>{
res.setHeader('Content-type','application/json');
const url = req.url;
req.path = url.split('?')[0]
const blogData = handleBlogRouter(req,res)
if(blogData){
res.end(JSON.stringify(blogData));
return;
}
const userData = handleUserRouter(req,res)
if(userData){
res.end(JSON.stringify(userData));
return;
}
res.writeHead(404,{'Content-type':'text/plain'});
res.write("404 Not Found\n");
res.end();
}
module.exports = serverHandle
package.json
{
"name": "blog-1",
"version": "1.0.0",
"description": "",
"main": "bin/www.js",
"bin": {
"blog-1": "www.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev":"cross-env NODE_ENV=dev nodemon ./bin/www.js",
"prd":"cross-env NODE_ENV=production nodemon ./bin/www.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.7"
}
}