Giter Club home page Giter Club logo

koa2-api's Introduction

KOA2 接口数据规范中间件

提供基于RestFul的生成接口数据的便捷方法

安装

$ npm install --save koa2-api

使用

创建中间件

const api = require('koa2-api')
app.use(api())

响应

定义了一个api属性挂载到了koa的ctx上,可以再控制器或路由直接取到ctx进行使用

ctx.body = { id: 1 }

改为

ctx.api.success({ id: 1 })

通用响应方法

api#success(data[, code])

成功响应,状态码默认200, 可通过 code 参数改变

api#error(data[, code])

错误响应,状态码默认404, 可通过 code 参数改变

api#created(localtion[, data])

资源已创建的响应,状态码默认201,可传入 localtion 参数,标识资源位置,将会在响应头中添加 Location 属性

api#updated(data)

资源已更新的成功响应,状态码默认200

api#deleted(data)

资源已删除的成功响应,状态码默认200

api#noContent()

无内容响应,状态码204

常用错误响应

api#errorBadRequest(message)

请求不合法错误, 状态码为400

api#errorUnauthorized(message)

未认证错误, 状态码为401

api#errorForbidden(message)

服务器拒绝错误, 状态码为403

api#errorNotFound(message)

没有找到资源的错误, 状态码为404

api#errorMethodNotAllowed(message)

方法不允许的错误, 状态码为405

api#errorNotAcceptable(message)

无法接受的类型, 状态码为406

api#errorUnavailable(message)

服务当前无法处理请求错误, 状态码为503

其他方法

api#addMeta(name, value)

以 key-value 的方式向响应数据的meta属性中添加属性

api#setMeta(meta)

设置响应数据的meta属性

api#addHeader(name, value)

向响应头添加header属性

以上属性支持链式调用: ctx.api.addMeta('name', 'zewail').success({ id: 1 })

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.