Use co-body directly instead of koa-body (which uses co-body under the hood)

This commit is contained in:
Tankred Hase 2017-08-27 16:32:25 +08:00
parent 1de83fe5d5
commit b1848bf8e6
5 changed files with 5 additions and 10 deletions

View File

@ -21,9 +21,9 @@
},
"dependencies": {
"addressparser": "^1.0.1",
"co-body": "^5.1.1",
"config": "^1.20.4",
"koa": "^2.3.0",
"koa-body": "^2.3.0",
"koa-router": "^7.2.1",
"koa-static": "^4.0.1",
"mongodb": "^2.2.31",

View File

@ -45,7 +45,6 @@ router.del('/api/v1/key', ctx => rest.remove(ctx));
app.on('error', middleware.logUnknownError);
app.use(middleware.upgradeToHTTPS);
app.use(middleware.setHTTPResponseHeaders);
app.use(middleware.parseBody());
app.use(router.routes());
app.use(router.allowedMethods());
app.use(serve(`${__dirname}/../static`));

View File

@ -19,7 +19,6 @@
const log = require('winston');
const config = require('config');
const koaBody = require('koa-body');
const util = require('../service/util');
exports.upgradeToHTTPS = async function(ctx, next) {
@ -51,11 +50,6 @@ exports.setHTTPResponseHeaders = async function(ctx, next) {
await next();
};
exports.parseBody = () => koaBody({
multipart: true,
formLimit: '1mb'
});
exports.logUnknownError = function(error, ctx) {
if (error.status) {
log.verbose('middleware', `Request failed: ${error.status} ${error.message}`);

View File

@ -17,6 +17,7 @@
'use strict';
const parse = require('co-body');
const util = require('../service/util');
/**
@ -37,7 +38,7 @@ class HKP {
* @param {Object} ctx The koa request/response context
*/
async add(ctx) {
const publicKeyArmored = ctx.request.body.keytext;
const {keytext: publicKeyArmored} = await parse.form(ctx, {limit: '1mb'});
if (!publicKeyArmored) {
ctx.throw(400, 'Invalid request!');
}

View File

@ -17,6 +17,7 @@
'use strict';
const parse = require('co-body');
const util = require('../service/util');
/**
@ -37,7 +38,7 @@ class REST {
* @param {Object} ctx The koa request/response context
*/
async create(ctx) {
const {publicKeyArmored} = ctx.request.body;
const {publicKeyArmored} = await parse.json(ctx, {limit: '1mb'});
if (!publicKeyArmored) {
ctx.throw(400, 'Invalid request!');
}