node+koa實(shí)現(xiàn)數(shù)據(jù)mock接口的方法
基于node+koa實(shí)現(xiàn)的mock數(shù)據(jù)接口,Koa需要v7.6.0以上node版本,低于此版本請(qǐng)先升級(jí)node
目錄結(jié)構(gòu)

// server.js
const Koa = require('koa');
const Router = require('koa-router');
const qs = require('qs');
const assert = require('assert');
const app = new Koa();
const router = new Router();
/**
* 獲取列表數(shù)據(jù)
* @param {request} page 頁(yè)數(shù)
* @param {request} limit 每頁(yè)數(shù)據(jù)條數(shù)
* @param {response} errno 返回狀態(tài)碼 0 ==> 返回成功 1 ==> 有錯(cuò)誤
* @param {response} hasMore 是否有更多數(shù)據(jù)
*/
let listData = require('./mock/list/list.js');
router.get('/api/getlist/:page/:limit', function (ctx, next) {
const page = ctx.params.page;
const limit = ctx.params.limit;
const maxPage = listData.length / limit;
// 構(gòu)造返回對(duì)象
let res = {
errno: 0,
data: {
hasMore: true,
data: []
}
};
// 如果超過最大頁(yè)面數(shù)
if ((page*1 + 1) >= maxPage) {
res.data.hasMore = false;
}
res.data.data = listData.slice(page*limit, page*limit + limit);
ctx.body = res;
});
/**
* 獲取詳情數(shù)據(jù)
* @param {request} id 商品id
*/
const detailData = require('./mock/detail/detail.js');
router.get('/api/getdetail/:id', function (ctx, next) {
const id = ctx.params.id
let res = {
errno: 0,
data: {
data: []
}
}
res.data.data = detailData;
// todo...
ctx.body = res;
});
/**
* 提交評(píng)論
* @param {request} id 用戶id
* @param {request} uid 商品id
* @param {request} msg 評(píng)論內(nèi)容
*/
router.post('/api/comment', function (ctx, next) {
const params = qs.parse(ctx.req._parsedUrl.query);
const id = params.id;
const uid = params.uid;
const msg = params.msg;
if (id === undefined || uid === undefined || msg === undefined) {
ctx.body = {
errno: 1,
msg: '缺少參數(shù)'
}
} else {
// todo...
ctx.body = {
errno: 0,
msg: '評(píng)論成功'
}
}
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log("server is running at http://localhost:3000/");
實(shí)際項(xiàng)目中,調(diào)用接口會(huì)遇到跨域的問題,解決的方式有多種,這里介紹如何在webpack中配置
module.exports = {
...
devServer: {
proxy: {
// 將 `/api` 開頭的 http 請(qǐng)求,都代理到 `localhost:3000` 上,由 koa 提供 mock 數(shù)據(jù)
'/api': {
target: 'http://localhost:3000',
secure: false
}
}
...
}
}
項(xiàng)目地址:https://github.com/daijingfeng/mock-server
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
NodeJS實(shí)現(xiàn)跨域的方法(使用示例)
CORS是一種 W3C 標(biāo)準(zhǔn),它使用額外的 HTTP 頭來告訴瀏覽器讓運(yùn)行在一個(gè) origin (domain) 上的Web應(yīng)用被準(zhǔn)許訪問來自不同源服務(wù)器上的指定的資源,這篇文章主要介紹了NodeJS實(shí)現(xiàn)跨域的方法,需要的朋友可以參考下2024-05-05
node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔示例
這篇文章主要介紹了node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Nodejs實(shí)現(xiàn)爬蟲抓取數(shù)據(jù)實(shí)例解析
這篇文章主要介紹了Nodejs實(shí)現(xiàn)爬蟲抓取數(shù)據(jù)實(shí)例解析,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
node schedule實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼
實(shí)際工作中,可能會(huì)遇到定時(shí)清除某個(gè)文件夾內(nèi)容,本文主要介紹了node schedule實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
基于uniapp與node.js實(shí)現(xiàn)的微信授權(quán)登錄功能實(shí)例
前端一直是一塊充滿驚喜的土地,不僅是那些富有創(chuàng)造性的頁(yè)面,還有那些驚贊的效果及不斷推出的新技術(shù),下面這篇文章主要給大家介紹了關(guān)于如何基于uniapp與node.js實(shí)現(xiàn)的微信授權(quán)登錄功能的相關(guān)資料,需要的朋友可以參考下2023-05-05
nodejs報(bào)digital?envelope?routines::unsupported錯(cuò)誤的最新解決方法
這篇文章主要介紹了nodejs報(bào)digital?envelope?routines::unsupported錯(cuò)誤的最新解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02

