question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Koa response 404 even though is succesful

See original GitHub issue

I am making an api with Koa:

This is the router handler: (assume I have router.get(…, get) and app.use(router.routes())

async function get(ctx, next){
  console.log(ctx.response)
  let user = await getUser(1);

  ctx.body = {
    data: user || [],
    message: ctx.response.status
  }
}

The api returns the user but I’ve noticed that even tough it matches the route, the data is returned as JSON and on the browser the response is 200, when I log the response is 404.

Is this a bug or more configuration is needed?

I am using Koa 2 (@next)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

70reactions
Oyelakingcommented, Jul 13, 2018

Figured this out from another thread. Basically, you need to search all your middleware functions and make sure that they are all async functions(Yes! Every single one of them attached to Koa).

27reactions
jemhuntrcommented, Aug 4, 2018

In addition to the above answer by @Oyelaking, make sure that none of your middlewares are prematurely returning without data.

In my case, I was calling jwt.verify without returning anything.

module.exports = async (ctx, next) => {
  const token = ctx.cookies.get('token');

  jwt.verify(token, config.auth.jwtSecret, (err, payload) => {
    if (err) {
      ctx.throw(401, {
        data: {
          error: 'UNAUTHORIZED',
          message: 'Invalid token.'
        }
      });
    } else {
      return next();
    }
  });
}

As you can see, I’m just calling jwt.verify() and I only call next() within a callback. This gets separated onto its own ‘thread’ and the middleware returns prematurely with no data on ctx.body.

I solved it by calling jwt.verify() with async/await.

  try {
    await jwt.verify(token, config.auth.jwtSecret);
    return next();
  } catch(err) {
    ctx.throw(401, {
      data: {
        error: 'UNAUTHORIZED',
        message: 'Invalid token.'
      }
    });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Koa Endpoint always return 404 - node.js - Stack Overflow
If I'm not mistaken, your SignUpAsync middleware should return a promise that Koa can await. Perhaps return db.AddUser(user).then( ? – Greg. Sep ...
Read more >
Could not post to Koa Route, always returning 404
I have setup a post route for saving API credentials for integration with our third-party service, but it always returns 404 not found....
Read more >
koajs/koa - Gitter
Hello! I'm having trouble with returning the result of an axios post request. I am able to make the request successfully: const result...
Read more >
How to Build a REST API with KoaJS - Crowdbotics
Simplifies the use of Error handling by using middleware more effectively; Identifies and understands all HTTP methods; Even before Express, Koa ...
Read more >
Using supertest to avoid manually testing your endpoints
The scenario: In order to test your endpoints/routes in Express/Koa/whatever you're using, you might currently be using a tool like Postman to send...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found