Home > Web Front-end > JS Tutorial > body text

node and koa implement data mock interface

小云云
Release: 2018-02-05 10:24:17
Original
1744 people have browsed it

This article mainly introduces the method of node+koa to implement the data mock interface. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Based on the mock data interface implemented by node+koa, Koa requires node version v7.6.0 or above. If it is lower than this version, please upgrade node first

Directory structure


// 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();

/**
 * 获取列表数据
 * @param {request} page 页数
 * @param {request} limit 每页数据条数
 * @param {response} errno 返回状态码 0 ==> 返回成功 1 ==> 有错误
 * @param {response} hasMore 是否有更多数据
 */
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;
  
  // 构造返回对象
  let res = {
    errno: 0,
    data: {
      hasMore: true,
      data: []
    }
  };

  // 如果超过最大页面数
  if ((page*1 + 1) >= maxPage) {
    res.data.hasMore = false;
  }
  res.data.data = listData.slice(page*limit, page*limit + limit);
   ctx.body = res;
});

/**
 * 获取详情数据
 * @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;
});

/**
 * 提交评论
 * @param {request} id 用户id
 * @param {request} uid 商品id
 * @param {request} msg 评论内容
 */
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: '缺少参数'
    }
  } else {
    // todo...
    ctx.body = {
      errno: 0,
      msg: '评论成功'
    }
  }
});

app
 .use(router.routes())
 .use(router.allowedMethods());
app.listen(3000);
console.log("server is running at http://localhost:3000/");
Copy after login

In actual projects, calling interfaces will encounter cross-domain problems. There are many ways to solve them. Here is how to configure them in webpack


module.exports = {
  ...

  devServer: {
    proxy: {
     // 将 `/api` 开头的 http 请求,都代理到 `localhost:3000` 上,由 koa 提供 mock 数据
     '/api': {
      target: 'http://localhost:3000',
      secure: false
     }
    }
    ...
  }
}
Copy after login

Project address: https://github.com/daijingfeng/mock-server

Related recommendations:

Example detailed explanation of vue using mock data

Detailed explanation of Mock file system in Node.js test

How does PHPUnit mock function Internally instantiated objects

The above is the detailed content of node and koa implement data mock interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template