Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Mark Herhold
    @alexmingoia Does koa-router offer any functionality to call a custom function on 404s and 501s? Or could this be better achieved with middleware of some sort?
    Sven Seitan
    @MarkHerhold I did something like that for 404 with koa-router and koa-ejs and path, if it helps:
      router = require('koa-router')(), 
      render = require('koa-ejs'),
      path = require('path');
    // ejs render
    render(app, {
      root: path.join(__dirname, 'public/views'),
      layout: 'layout',
      viewExt: 'ejs',
      cache: false,
      debug: true
    // routes
      .get('/', function *(){
        console.log('> Viewing homepage.');
        yield this.render('homepage', {title: "My website"});
      .get('/documentation', function *(){
        console.log('> Viewing documentation.');
        yield this.render('documentation', {title: "Documentation - My website"});
    app.use(function *(){
    Mark Herhold
    @svenseitan nice! I'll give this a shot.
    Mark Herhold
    @svenseitan Worked like a charm. Thanks!
    Dexter Miguel
    hey @alexmingoia, I was having a lot of trouble using Router.prototype.use without a URL and I figured out the issue. i've opened a PR whenever you wanna take a look.
    suren shrestha

    I have following simple router

      .get('/', function *(next) {
        let p = Post.findAll();
        p.then((posts) => {
            this.body = posts;
        p.catch(function (err) {
            this.body = err;

    I was hoping to see the list of posts when i point my browser to / but I get Not Found instead.

    Mark Herhold
    @ludbek I think the issue is with how you are using promises. Basically you aren't waiting (yield) for the result and this.body is not set to anything by the time koa responds. Try:
      .get('/', function *(next) {
        yield Post.findAll().then((posts) => {
            this.body = posts;
        }).catch(function (err) {
            this.body = err;
    or event better:
    router.get('/', function *() {
        try {
            this.body = yield Post.findAll();
        } catch(err) {
            this.status = 500; // internal server error status code
            this.body = err.message; // not a good idea in real life
    suren shrestha
    @MarkHerhold Silly mistake, I completely forgot about generators back then. Thanx :)
    Christopher Shelley
    Hi all, can anyone tell me how to make my templates in /views/ pick up their resources in /public/ as though they were also in /public/ ?
    Christopher Shelley
    ( it was koa-static )
    Jeff Mealo
    Hello all, if I could get eyes on this it'd be much appreciated, I've been spinning my wheels for an hour or two without much luck: alexmingoia/koa-router#270
    Drake Leung
    This message was deleted
    This message was deleted
    Alex Saft
    Hi everyone!
    Thankes for the lib!
    @alexmingoia or anyone else, could you provide mini-example of koa-router with koa@2?
    const router = require('koa-router')();
    const koaConvert = require('koa-convert');
    const koaRouter = require('koa-router');
    const router = koaConvert(koaRouter)();
    //and the result is:
    //koa deprecated Support for generators will been removed in v3. 
    //See the documentation for examples of how to convert old middleware 
    const router = koaConvert(koaRouter());
    //just throws the error on the app start:
    //TypeError: middleware must be a function
    Pavel Kolář
    Hello, I am wrapping my head around how to use my simple authorization middleware on specific routes. I know I can do router.use(['/users', '/admin'], userAuth()) but that doesn't help I want to let user to GET /users but not DELETE /user (bad example I admit) but with the way how koa middleware works I can't really distinguish between these two. So what I am doing now is copying the same helper function checkUserAuth(ctx) to routes which require authorization and it doesn't feel right at all
    Daniel Kanas
    does koa-router support query strings? I can access ctx.query in normal middleware, but when using koa-router ctx.query returns an empty object
    Alex Pilon
    hey everyone, this is a micro-optimization that probably isn't a big deal, but could we mount a bodyParser ONLY on request methods that need it (POST, PATCH, etc)
    Guilherme Bayer
    Hi guys, i have a question, as i run a middleware on various routes?
    Nikolai Hegelstad
    Hi, I'm new to Koa, should i use the stable version or the latest 7.x.x?
    Same goes for koa-router, and do they have to be the same "version"?
    Ricardo Canastro

    I would like to run a function before and after all routes.. something like this:

    router.use('*', function *(next) {
                yield next;
            router.use('*', function *(next) {
                yield next;

    Do you guys know how to do something like this?

    I add a timestamp to all response headers
    app.use(async (ctx, next) => {
        const start = new Date();
        await next();
        const ms = new Date() - start;
        ctx.set('X-Response-Time', `${ms}ms`);
    router.post('/register', (ctx) => {
    body undefined
    could someone help me ?
    or could someone tell me how to use the post ?(demo) i need it request.body
    i can find it in the req.request
    but in these the ctx become a generator
    Oh no i find i am use the false version
    Gabriel Schmitt
    hey guys I'm in trouble.. do you know why router.use doesn't inject the route params into ctx.params ?
    do I have to use router.param instead?
    not sure if I understood why this .param is for
    Andy Johnson
    how would I remove a router from a running instance of koa, and re-initialize it? trying to get something working with HMR and I only want to replace the router instance
    Why does downstream middleware still run after a route match???
    Hi. Is there an automatic docs generator for koa-router?
    Josh Bielick
    Javier Aviles

    Hi! I wanted to leave here a good boilerplate with Koa2 Typescript, koa-router, jwt logging orm sql docker.... Very good Readme, hope it helps somebody!


    Manthan Mallikarjun
    Hey everyone
    Please take a look at this issue
    Stanley Makazhe
    Hello, can someone explain why this code won't work router.post("/", checkJwt, Ctrl.create);
    Possible you have not connected your router to koa instance or checkJwt and Ctrl.create does not match koa middleware signature