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)文章
node.js express安裝及示例網(wǎng)站搭建方法(分享)
下面小編就為大家?guī)硪黄猲ode.js express安裝及示例網(wǎng)站搭建方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
使用iojs的jsdom庫(kù)實(shí)現(xiàn)同步系統(tǒng)時(shí)間
本文給大家分享的是使用iojs的jsdom庫(kù)實(shí)現(xiàn)與http://open.baidu.com/special/time/ 同步系統(tǒng)時(shí)間。思路非常的清晰,這里推薦給大家,有需要的小伙伴可以參考下。2015-04-04
基于Node的Axure文件在線預(yù)覽的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于Node的Axure文件在線預(yù)覽的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Node.js 應(yīng)用跑得更快 10 個(gè)技巧
Node.js 受益于它的事件驅(qū)動(dòng)和異步的特征,已經(jīng)很快了。本文將介紹 10 條,經(jīng)過檢驗(yàn)得知可大大提高 Node 應(yīng)用的技巧。廢話不多說,讓我們逐條來看看2016-04-04
淺析node應(yīng)用的timing-attack安全漏洞
本篇文章給大家通過原理的原因分析了node應(yīng)用的timing-attack安全漏洞問題,有興趣的朋友閱讀參考下。2018-02-02
npm安裝sharp出現(xiàn)的問題詳解(安裝失敗的問題及解決)
這篇文章主要給大家介紹了關(guān)于npm安裝sharp出現(xiàn)的問題(安裝失敗的問題及解決)的相關(guān)資料,sharp包是基于node.js的高性能圖片處理器,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11

