最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

node+koa實(shí)現(xiàn)數(shù)據(jù)mock接口的方法

 更新時(shí)間:2017年09月20日 14:39:49   作者:五月的星空  
本篇文章主要介紹了node+koa實(shí)現(xiàn)數(shù)據(jù)mock接口的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

基于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: []
    }
  };

  // 如果超過(guò)最大頁(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 用戶(hù)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ì)遇到跨域的問(wèn)題,解決的方式有多種,這里介紹如何在webpack中配置

module.exports = {
  ...

  devServer: {
    proxy: {
     // 將 `/api` 開(kāi)頭的 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 文本操作模塊-fs模塊(四)

    詳解nodejs 文本操作模塊-fs模塊(四)

    本篇文章詳細(xì)的講訴fa.fstat方法,這個(gè)State對(duì)象中,包含的數(shù)據(jù)都有哪些,并且他們分別代表的含義是什么。具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • NodeJS實(shí)現(xiàn)跨域的方法(使用示例)

    NodeJS實(shí)現(xiàn)跨域的方法(使用示例)

    CORS是一種 W3C 標(biāo)準(zhǔn),它使用額外的 HTTP 頭來(lái)告訴瀏覽器讓運(yùn)行在一個(gè) origin (domain) 上的Web應(yīng)用被準(zhǔn)許訪(fǎng)問(wèn)來(lái)自不同源服務(wù)器上的指定的資源,這篇文章主要介紹了NodeJS實(shí)現(xiàn)跨域的方法,需要的朋友可以參考下
    2024-05-05
  • 在Ubuntu系統(tǒng)上安裝Node.JS的教程

    在Ubuntu系統(tǒng)上安裝Node.JS的教程

    這篇文章主要介紹了在Ubuntu系統(tǒng)上安裝Node.JS的教程,Node.JS的高性能V8解釋器運(yùn)行及異步機(jī)制為其帶來(lái)了巨大的人氣,需要的朋友可以參考下
    2015-10-10
  • node?puppeteer爬蟲(chóng)爬取電影網(wǎng)站及生成pdf文檔示例

    node?puppeteer爬蟲(chóng)爬取電影網(wǎng)站及生成pdf文檔示例

    這篇文章主要介紹了node?puppeteer爬蟲(chóng)爬取電影網(wǎng)站及生成pdf文檔使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Node.js中path模塊操作路徑的基本使用

    Node.js中path模塊操作路徑的基本使用

    這篇文章主要介紹了Node.js中path模塊操作路徑的基本使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Nodejs實(shí)現(xiàn)爬蟲(chóng)抓取數(shù)據(jù)實(shí)例解析

    Nodejs實(shí)現(xiàn)爬蟲(chóng)抓取數(shù)據(jù)實(shí)例解析

    這篇文章主要介紹了Nodejs實(shí)現(xiàn)爬蟲(chóng)抓取數(shù)據(jù)實(shí)例解析,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • node schedule實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    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í)例

    基于uniapp與node.js實(shí)現(xiàn)的微信授權(quán)登錄功能實(shí)例

    前端一直是一塊充滿(mǎn)驚喜的土地,不僅是那些富有創(chuàng)造性的頁(yè)面,還有那些驚贊的效果及不斷推出的新技術(shù),下面這篇文章主要給大家介紹了關(guān)于如何基于uniapp與node.js實(shí)現(xiàn)的微信授權(quán)登錄功能的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • node.js中的console.warn方法使用說(shuō)明

    node.js中的console.warn方法使用說(shuō)明

    這篇文章主要介紹了node.js中的console.warn方法使用說(shuō)明,本文介紹了console.warn的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • nodejs報(bào)digital?envelope?routines::unsupported錯(cuò)誤的最新解決方法

    nodejs報(bào)digital?envelope?routines::unsupported錯(cuò)誤的最新解決方法

    這篇文章主要介紹了nodejs報(bào)digital?envelope?routines::unsupported錯(cuò)誤的最新解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02

最新評(píng)論

嵩明县| 乌鲁木齐市| 永胜县| 长岭县| 云梦县| 迁西县| 兖州市| 通化市| 施甸县| 台江县| 宁武县| 正蓝旗| 池州市| 博兴县| 赤水市| 水城县| 章丘市| 梨树县| 迁安市| 呈贡县| 云浮市| 清河县| 溆浦县| 西盟| 建始县| 崇文区| 南投县| 天全县| 顺平县| 石柱| 绩溪县| 当雄县| 龙山县| 十堰市| 红河县| 应城市| 华容县| 卢氏县| 南通市| 乌鲁木齐县| 离岛区|