resModel的代码
class BaseModel {
constructor(data, message) {
if(typeof data === 'string' && typeof message === 'string'){
throw new Error("two String params,only one params")
}
if (typeof data === 'string') {
this.message = data
data = null
message = null
}
if (data) {
this.data = data
}
if (message) {
this.message = message
}
}
}
class SuccessModel extends BaseModel{
constructor(data,message){
super(data,message)
this.errno = 0
}
}
class ErrorModel extends BaseModel{
constructor(data,message){
super(data,message)
this.errno = -1
}
}
module.export ={
SuccessModel,
ErrorModel
}
router/blog的代码
const {getList} = require('../controller/blog')
const {SuccessModel,ErrorModel} = require('../model/resModel')
const handleBlogRouter = (req,res) => {
const method = req.method
if(method === 'GET' && req.path === "/api/blog/list"){
const author = req.query.author || ''
const keyword = req.query.keyword || ''
const listdata = getList(author,keyword)
return new SuccessModel(listdata)
}
if(method === 'GET' && req.path === "/api/blog/detail"){
return{
msg:'获取博客详情接口'
}
}
if(method === 'POST' && req.path === "/api/blog/del"){
return{
msg:'删除博客接口'
}
}
if(method === 'POST' && req.path === "/api/blog/new"){
return{
msg:'新建博客接口'
}
}
if(method === 'POST' && req.path === "/api/blog/update"){
return{
msg:'更新博客接口'
}
}
}
module.exports = handleBlogRouter
controller/blog代码
const getList = (author,keyword) => {
return
[
{
id:1,
title:'标题A',
content:'内容A',
createTime:1659146935340,
author:'zhangsan'
},
{
id:2,
title:'标题A',
content:'内容A',
createTime:1659146947276,
author:'lisi'
}
]
}
module.exports={
getList
}