Simplified HTTP request for Koa(2.0+).
npm install koa-http-request --save
const koa = require('koa');
const koaRequest = require('koa-http-request');
const app = new koa();
app.use(koaRequest({
json: true, //automatically parsing of JSON response
timeout: 3000, //3s timeout
host: 'https://api.github.com'
}));
app.use(async (ctx, next) => {
let repo = await ctx.get('/repos/junyiz/koa-http-request', null, {
'User-Agent': 'koa-http-request'
});
ctx.body = 'repos id: ' + repo.id + '\nrepos name: ' + repo.full_name;
});
app.listen(process.env.PORT || 8090);
Type: 'Boolean' Default: false
Parse response body with JSON.parse
and set accept
header to application/json
.
Type: number
Default: 30000
Milliseconds to wait for a server to send response headers before aborting request with ETIMEDOUT
error.
Type: 'String' Default: ''
MIT