This is the 9th day of my participation in Gwen Challenge
This article is the Node learning series of the third article, followed by the previous article, the basic completion of routing processing, learn to use Node interface development ~
Get/API /blog/list/get/API /blog/list
controller/blog.js
.const getDetail = id= > {
return {
id: 1.title: 'title XXX'.content: Content of the 'A'.createTime: 1623165670787.author: 'Tmier1'}}module.exports = {
...
getDetail
}
Copy the code
router/blog.js
const { getList, getDetail } = require('.. /controller/blog')...// Get blog details
if (method == 'GET' && req.path == '/api/blog/detail') {
const id = req.query.id
const data = getDetail(id)
return new SuccessModel(data)
}
Copy the code
OK, the GET request is simple for now, and then we’ll focus on the POST request.
Encapsulation method
Briefly encapsulate how to handle POST request data
app.js
// Used to process postData
const getPostData = req= > {
return new Promise((resolve, reject) = > {
if(req.method ! = ='POST') {
resolve({})
return
}
// Non-JSON data type, ignore and return {}
if (req.headers['content-type'! = ='application/json']) {
resolve({})
return
}
/ / correct
let postData = ' '
req.on('data'.chunk= > {
postData += chunk.toString()
})
req.on('end'.() = > {
if(! postData) { resolve({})return
}
// Successful return
resolve(JSON.parse(postData))
})
})
}
Copy the code
Then put the postData into req.body, complete app.js as follows:
const querystring = require('querystring')
const handleBlogRouter = require('./src/router/blog.js')
const handleUserRouter = require('./src/router/user.js')
// Used to process postData
const getPostData = req= > {
return new Promise((resolve, reject) = > {
if(req.method ! = ='POST') {
resolve({})
return
}
// Non-JSON data type, ignore and return {}
if (req.headers['content-type'! = ='application/json']) {
resolve({})
return
}
/ / correct
let postData = ' '
req.on('data'.chunk= > {
postData += chunk.toString()
})
req.on('end'.() = > {
if(! postData) { resolve({})return
}
// Successful return
resolve(JSON.parse(postData))
})
})
}
const serverHandle = (req, res) = > {
res.setHeader('Content-Type'.'application/json')
/ / get the path
const url = req.url
req.path = url.split('? ') [0]
/ / query
req.query = querystring.parse(url.split('? ') [1])
/ / postData processing
getPostData(req).then(postData= > {
req.body = postData
// Handle blog routing
const blogData = handleBlogRouter(req, res)
if (blogData) {
res.end(JSON.stringify(blogData))
return
}
// Process the user route
const userData = handleUserRouter(req, res)
if (userData) {
res.end(JSON.stringify(userData))
return
}
// No route is matched, 404 is returned
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.write('404 Not Found\n')
res.end()
})
}
module.exports = serverHandle
Copy the code
A new blog
controller/blog.js
Simulated data, exposed methods
.const newBlog = (blogData = {}) = > {
return {
id: 3}}...module.exports = {
...
newBlog,
}
Copy the code
router/blog.js
const { getList, getDetail, newBlog, updateBlog} = require('.. /controller/blog')
const { SuccessModel, ErrorModel } = require('.. /model/resModel')...// Create a new blog
if (method == 'POST' && req.path == '/api/blog/new') {
const data= newBlog(req.body)
return new SuccessModel(data)
}
Copy the code
Update the blog
controller/blog.js
.const updateBlog = (id,blogData= {}) = > {
return true
}
module.exports = {
...
updateBlog
}
Copy the code
router/blog.js
const id = req.query.id
// Update a blog post
if (method == 'POST' && req.path == '/api/blog/update') {
const result = updateBlog(id,req.body)
if(result) {
return new SuccessModel()
} else {
return new ErrorModel('Failed to update blog ~')}}Copy the code
Delete the blog
controller/blog.js
const delBlog = (id) = > {
// id: id of the blog to be deleted
return true}...module.exports = {
...
delBlog
}
Copy the code
router/blog.js
// Delete a blog post
if (method == 'POST' && req.path == '/api/blog/del') {
const result = delBlog(id)
if (result) {
return new SuccessModel()
} else {
return new ErrorModel('Failed to delete blog ~')}}Copy the code
Login interface
Create user.js in the crotroller folder to process the data returned by the login interface
croller/user.js
const loginCheck = (username, password) = > {
if(username == 'lll' && password == '666') {
return true
}
return false
}
module.exports = {
loginCheck
}
Copy the code
user.js
const { loginCheck } = require('.. /controller/user')
const { SuccessModel, ErrorModel } = require('.. /model/resModel')
const handleUserRouter = (req, res) = > {
const method = req.method
/ / login
if (method == 'POST' && req.path == '/api/user/login') {
const { username, password } = req.body
const result = loginCheck(username,password)
if(result) {
return new SuccessModel()
}
return new ErrorModel('Login failed ~')}}module.exports = handleUserRouter
Copy the code
OK, to this even if the routing processing is completed ~ here the routing processing is similar, write more practice a few times good, using Node to write interface is comfortable ~