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.

Receiving 404 "Not Found" but i'm setting ctx.response.body = token

See original GitHub issue

Hi!

I keep getting 404 "Not Found" while i’ve already set ctx.response.body = token *where toke is a valid token in my tests (with supertest & in postman).

Is there a way to publish the token and override the “Not found” message?

Here is how i’m making the token

const signUp = async (ctx, next) => {
    
    const bodyInfo = await ctx.request.body;

    if(!bodyInfo.username || !bodyInfo.password) {
        ctx.status = 402;
        ctx.body = "Error, username and password must be provided!";
    }

    const userInst = new User(bodyInfo);

    userInst.save(async(err, user) => {
        
        if(err) { return next(err); }

        const token = tokenForUser(user);

        ctx.body = token; //- <-- Passing the token.

        return next();
    });
};

export { signUp, signIn };

the route that i hit to get this middleware is

router.post('/signup', signUp);

//- oddly enough

router.post('/signup', signUp, async (ctx, next) => {
    const res = await ctx.response;
    
     //- returns a lot of good stuff including `token` but can't publish token outside.
     console.log(res); 

I wish to override “Not Found” message and the whole ctx.response cause i get really long weird pre made 404s for a route that is preset.

Here is my stack trace.

  1) Authentication signs up:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules/supertest/lib/test.js:266:12)
      at Test._assertFunction (node_modules/supertest/lib/test.js:281:11)
      at Test.assert (node_modules/supertest/lib/test.js:171:18)
      at assert (node_modules/supertest/lib/test.js:131:12)
      at node_modules/supertest/lib/test.js:128:5
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:631:3)
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:795:18)
      at endReadableNT (_stream_readable.js:974:12)
      at _combinedTickCallback (internal/process/next_tick.js:74:11)
      at process._tickCallback (internal/process/next_tick.js:98:9)

Please help and if i’m missing anything i asked this question on stackoverflow here.

Thank you.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
EduardoRFScommented, Jan 11, 2017
    userInst.save(async(err, user) => {
        
        if(err) { return next(err); }

        const token = tokenForUser(user);

        ctx.body = token; //- <-- Passing the token.

        return next();
    });
};

this is wrong, use userInst.save like a promise

const user = await userInst.save();
const token = tokenForUser(user);

ctx.body = token;

return next()

if your database manager dont support promises, build your own promise

1reaction
iedmrccommented, Oct 21, 2020

in my case, it was about calling await next() at the end of the middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

koa.js returning 404 "Not Found" after signup, instead of the ...
I'm trying to get the jwt from body so i can make authenticated requests, but for some reason i keep on getting "Not...
Read more >
404 error using node.js checkoutAPI - Square Developer
I'm trying to use square hosted checkout for my pizza business. It looks as though the data I'm sending is valid, as I...
Read more >
Responses | Strapi Documentation
Whenever a stream is set as the response body, .onerror is automatically added as a listener to the error event to catch any...
Read more >
Sending Response with Koa - Medium
We can send response status codes by setting the status code value to the responbse.status property. ... ctx.status = 202; ctx.body = 'accepted';...
Read more >
KOA route return 404 not found - Shopify Community
Bumping, I followed the tutorial and setup my route like so: //server.js router.get('/products', verifyRequest(), async (ctx) => {¬ await handle ...
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