Koa response 404 even though is succesful
See original GitHub issueI 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:
- Created 7 years ago
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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).
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.As you can see, I’m just calling
jwt.verify()
and I only callnext()
within a callback. This gets separated onto its own ‘thread’ and the middleware returns prematurely with no data onctx.body
.I solved it by calling
jwt.verify()
with async/await.