koajs / joi-router

Configurable, input and output validated routing for koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

body parser err

willin opened this issue · comments

const Koa = require('koa');
const router = require('koa-joi-router');
const Joi = require('joi');
const parse = require('co-body');

const routes = router();

routes.route([
  {
    method: 'post',
    path: '/signup',
    validate: {
      body: {
        name: Joi.string().max(100),
        email: Joi.string().lowercase().email(),
        password: Joi.string().max(100),
        _csrf: Joi.string().token()
      },
      type: 'form'
    },
    handler: async (ctx) => {
      console.log('sth'); // does not print 
      ctx.status = 200;
      ctx.body = ctx.request.body;
    }
  }
]);

const app = new Koa();

// add this for test.
app.use(async (ctx, next) => {
  ctx.request.body = await parse.form(ctx);
  console.log(ctx.request.body);
  // curl -d "name=test" http://localhost:3000/signup
  // ^@curl: (52) Empty reply from server
  // print: { name: 'test' }
  await next(); // always pendding
});

app.use(routes.middleware());
app.listen(3000);

i need a pre-request-handler to check whether the token is valid.

so i have 3 questions:

  1. how to fix this issue
  2. how can i parse the body before jump into the router
  3. how to support multiple validate.type like ['json','form']

joi-router has body-parser

co-body i no...