• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jeffijoe/koa-respond: Koa middleware that adds useful methods to the context.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jeffijoe/koa-respond

开源软件地址:

https://github.com/jeffijoe/koa-respond

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-respond

npm dependency Status devDependency Status Build Status Coveralls Code Climate npm license node JavaScript Style Guide KoaJs Slack

Middleware for Koa that adds useful methods to the Koa context.

Installation

npm install --save koa-respond

Usage

// Install it
const respond = require('koa-respond');

// For Koa v2 - if you are looking for v1, scroll to the bottom.
app.use(respond());

// Use it
app.use((ctx) => {
  // Sets status to 200 and the body to `{ id: 123, name: 'Dat Boi' }`
  ctx.ok({ id: 123, name: 'Dat Boi' });

  // Both of these set status to 404 and
  // the body to `{ message: 'Not found, boii' }`
  ctx.notFound('Not found, boii');
  ctx.notFound({ message: 'Not found, boii' });

  // And everyone's favorite..
  ctx.badRequest({ error: 'missing input' });

  // Or if you prefer to do it yourself..
  // Both of these send a HTTP 201 with a body
  // of `{ message: 'new beginnings!' }`
  ctx.send(201, 'new beginnings!');
  ctx.send(201, { message: 'new beginnings!' });
});

Methods

All methods call the send method with the corresponding status code as well as the body. That means they support the same overloads as send:

  • With a string; wraps it in an object with a message property. That means the following 2 calls do the same thing:

    ctx.send(400, 'lol no');
    ctx.send(400, { message: 'lol no' });
  • With an object; sends the object as JSON.

    ctx.send(200, { id: 123, name: 'new entity' });

If you wish to disable the automatic wrapping of strings globally, you can instantiate koa-respond with autoMessage: false.

app.use(respond({
  autoMessage: false
}))

All functions return the Koa context itself (chainable)

ctx.ok().set({ 'X-Some-Header': 'awesome' })

All functions are also bound to the context. This means you can pass the function as a reference without having to bind it first.

app.use((ctx) => somePromiseCall().then(ctx.ok))

Available methods

  • ok - HTTP 200
  • created - HTTP 201
  • noContent - HTTP 204 - always sends an empty response!
  • badRequest - HTTP 400
  • unauthorized - HTTP 401
  • forbidden - HTTP 403
  • notFound - HTTP 404
  • locked - HTTP 423
  • internalServerError - HTTP 500
  • notImplemented - HTTP 501

Does this work for Koa 1?

Not out of the box, because it's time you move on to v2.

To use koa-respond in Koa v1, you need to patch the context yourself. This is what the v2 middleware does.

const respond = require('koa-respond');

// Middleware to install koa-respond.
app.use(function *(next) {
  respond().patch(this);
  yield next;
});

// Now the methods are available.
app.use(function *() {
  this.ok({ id: 123, name: 'Bob' });
});

Adding additional methods

If you feel like some methods are missing, you can add them yourself, like so:

app.use(respond({
  statusMethods: {
    imATeapot: 418,
    enhanceYourCalm: 420
  }
}));

app.use((ctx) => {
  ctx.imATeapot('Hello, a Teapot I am.');
  ctx.enhanceYourCalm({ todo: 'blaze it' });
});

Even more custom methods

If you just want to add shortcuts without adding an additional middleware, you can do that, too.

app.use(respond({
  methods: {
    shizzle: (ctx, message) => {
      ctx.send(200, message + ', fo-shizzle');
    }
  }
}));

app.use((ctx) => {
  // HTTP 200 { message: 'Koa is the best, fo-shizzle' }
  ctx.shizzle('Koa is the best');
});

Contributing

npm run scripts

  • npm run test: Runs tests once
  • npm run test-watch: Runs tests in watch-mode
  • npm run lint: Lints the code once
  • npm run lint-watch: Lints the code in watch-mode
  • npm run cover: Runs code coverage using istanbul
  • npm run coveralls: Used by coveralls

Author

Jeff Hansen - @Jeffijoe




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
octatone/koa-webpack-hot-middleware发布时间:2022-07-09
下一篇:
Selvin11/koa2-login: A login project's backend is koa2 and login by Github. ...发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap