Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Alex Mingoia
    @alexmingoia
    @BDiehr I can't reproduce that. What version of koa-router are you using?
    Mark Herhold
    @MarkHerhold
    @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
    @svenseitan
    @MarkHerhold I did something like that for 404 with koa-router and koa-ejs and path, if it helps:
    const
      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
    router
      .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(router.routes())
      .use(router.allowedMethods());
    
    app.use(function *(){
      this.redirect('/404');
    });
    Mark Herhold
    @MarkHerhold
    @svenseitan nice! I'll give this a shot.
    Mark Herhold
    @MarkHerhold
    @svenseitan Worked like a charm. Thanks!
    Dexter Miguel
    @divmgl
    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.
    alexmingoia/koa-router#163
    suren shrestha
    @ludbek

    I have following simple router

    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
    @MarkHerhold
    @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:
    router
      .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
    @ludbek
    @MarkHerhold Silly mistake, I completely forgot about generators back then. Thanx :)
    Christopher Shelley
    @basiclaser
    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
    @basiclaser
    ( it was koa-static )
    Jeffrey Mealo
    @jmealo
    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
    @lyyourc
    This message was deleted
    This message was deleted
    Alex Saft
    @fend25
    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')();
    //or
    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 
    //https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x
    const router = koaConvert(koaRouter());
    //just throws the error on the app start:
    //TypeError: middleware must be a function
    Pavel Kolář
    @kolpav
    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
    @dkanas
    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
    @MadMub
    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
    @iamgbayer
    Hi guys, i have a question, as i run a middleware on various routes?
    Nikolai Hegelstad
    @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"?
    SmallW
    @erbing
    hello?
    Anthony Ettinger
    @chovy
    hello
    Ricardo Canastro
    @canastro
    Hello

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

    router.use('*', function *(next) {
                logger.debug('BEFORE');
                yield next;
            });
            app.use(router.routes());
            app.use(router.allowedMethods());
            router.use('*', function *(next) {
                logger.debug('AFTER');
                yield next;
            });

    Do you guys know how to do something like this?

    Anthony Ettinger
    @chovy
    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`);
      });
    peisong
    @bringtree
    app.use(bodyParser());
    router.post('/register', (ctx) => {
      console.log(ctx.request.body);
    })
    body undefined
    peisong
    @bringtree
    could someone help me ?
    peisong
    @bringtree
    or could someone tell me how to use the post ?(demo) i need it request.body
    i can find it in the req.request
    this.req.request
    peisong
    @bringtree
    but in these the ctx become a generator
    peisong
    @bringtree
    Oh no i find i am use the false version
    :(
    Gabriel Schmitt
    @gabrielsch
    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
    @andyrj
    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
    cquillen2003
    @cquillen2003
    Why does downstream middleware still run after a route match???
    Joachim Seminck
    @jseminck
    Hi. Is there an automatic docs generator for koa-router?
    Josh Bielick
    @jbielick
    Yes
    Javier Aviles
    @javieraviles

    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!

    https://github.com/javieraviles/node-typescript-koa-rest

    Manthan Mallikarjun
    @nahtnam_gitlab
    Hey everyone
    Please take a look at this issue
    Stanley Makazhe
    @devt3000
    Hello, can someone explain why this code won't work router.post("/", checkJwt, Ctrl.create);