在前后端同步開發(fā)的過程中尤仍,前期后臺(tái)接口出的沒那么快逞带,前端一般可以自己通過koa模擬后臺(tái)接口—前提時(shí)與后臺(tái)約定好數(shù)據(jù)格式。下面是如何用koa2模擬假數(shù)據(jù)接口的實(shí)現(xiàn):
首先安裝個(gè)koa腳手架
npm install koa-generator -g
koa生成一個(gè)server目錄并安裝依賴
koa2 server && cd server&& npm i
新建個(gè)假數(shù)據(jù)目錄
mkdir mock
目錄
DetailData的假數(shù)據(jù)長這樣
module.exports = {
"msg": "成功",
"code": 200,
"data": {
"records": [{
"id": 149,
"alertName": "poseidon_httpClient_codeCountStat_count_total",
"buCode": "test",
"buName": "中間件",
"projectCode": "test-public",
"projectName": "中間件公共項(xiàng)目",
"resourceType": "java",
"resourceCode": "service_code_4431",
"resourceName": "mssp-web",
"alertStatus": 0,
"alertStatusName": "處理中",
"updateTime": "2022-03-23 14:57:57"
}],
"total": 66,
"size": 10,
"current": 1,
"orders": [
],
"optimizeCountSql": true,
"hitCount": false,
"countId": null,
"maxLimit": null,
"searchCount": true,
"pages": 7
},
"success": true,
"exception": null,
"attachment": null,
"errCode": 200
}
alarm.js里的長這樣
const router = require('koa-router')()
let DetailData = require('../mock/historyDingTalkDetail')
router.get('/alert/history/dingTalk/detail', async (ctx, next) => {
ctx.append("Access-Control-Allow-Origin","*")
ctx.body = DetailData
})
module.exports = router
然后app.js里面的其它的不需要?jiǎng)铀鳎皇桥渲孟侣酚删托?/p>
// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())
app.use(alarm.routes(), alarm.allowedMethods())
好了界斜,接下來
npm run dev
// 或者
npm run start
// 都行,一個(gè)時(shí)修改熱加載司忱,一個(gè)要重啟皇忿,看需求