Giter Site home page Giter Site logo

koa-server-module's Introduction

koa-server-module

快速构建一个 koa 接口服务器。

Install

npm install --save koa-server-module

Quick start

快速上手,使用简单语法糖快速构建一个服务。

// get method
server.get([address],[response]);

// post method
server.post([address],[response]);

example.js

import {server} from "koa-server-module";

server.get('/get', {
  code: 200,
  msg: 'get success',
  data: null
});

server.post('/post', {
  code: 200,
  msg: 'post success',
  data: null
});

server.listen(9000);

使用es6的写法需要使用 babel-register 来运行此js文件。 浏览器打开 http://localhost:9000/get, 得到对应的json数据

另一种写法

koa-router 的写法。

import {server, router} from "koa-server-module";

router.get('/get', async(ctx,next) => {
    ctx.body = JSON.stringify({
        code: 200,
        msg: 'get success',
        data: null
    })
});

router.post('/post', async(ctx,next) => {
    ctx.body = JSON.stringify({
        code: 200,
        msg: 'post success',
        data: null
    })
});

server.listen(9000);

middleware

使用中间件,例如给每个接口设置header。

import {server, router} from "koa-server-module";

const setHeader = async(ctx, next) => {
  ctx.set(`Access-Control-Allow-Credentials`, true);
  await next();
};

router.get('/get', setHeader, async(ctx,next) => {
    ctx.body = JSON.stringify({
        code: 200,
        msg: 'get success',
        data: null
    })
});

server.listen(9000);

Delay

延迟响应,模拟慢网速

import {server} from "koa-server-module";

server.delay(3000); // 延迟3秒

server.get('/get', {
  code: 200,
  msg: 'get success',
  data: null
});

server.listen(9000);

pagination

分页,获取请求的数据并返回数据。

example

import {server, router} from "koa-server-module";

router.get(`/page`, async(ctx, next) => {
  const pageIndex = ctx.query.pageIndex ? parseInt(ctx.query.pageIndex) : 1;
  const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 1;
  ctx.body = JSON.stringify(
    {
      "code": 200,
      "msg": "success",
      "data": {
        pageIndex: pageIndex,
        pageSize: pageSize,
        totalPage: Math.ceil(3 / pageSize),
        list: [
          {
            id: 1,
            name: 1
          },
          {
            id: 2,
            name: 2
          },
          {
            id: 3,
            name: 3
          }
        ].splice((pageIndex - 1) * pageSize, pageSize)
      }
    }
  );
});

server.listen(9000);

es5 way

es5环境下如何使用

'use strict';

const server = require('../build/server').server;

server.get('/test',{
  code: 200,
    msg: 'success',
    data: null
});

server.listen(9001);

koa-server-module's People

Stargazers

 avatar Basic Wang avatar Alarm1673 avatar Mason_ZZ avatar Zhangxinlin avatar XiangyuShi avatar

Watchers

James Cloos avatar

koa-server-module's Issues

babel的依赖应该装在 devDependencies 里面

"dependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-polyfill": "^6.20.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-0": "^6.16.0",
}

如上,这样的话,项目里也会装入上方的依赖了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.