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.

Response headers will be ignored/removed while using ctx.throw.

See original GitHub issue
ctx.set( 'Access-Control-Allow-Origin', '*' );
ctx.throw( 404 );

By using the code above, the header of Access-Control-Allow-Origin will not be sent with responding. Even though I can use res.statusCode instead, but I think the ctx.throw method should not remove response headers.

Koa@2.4.1 Node@8.9.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mutoecommented, Mar 31, 2018

According to my usage, you also put it in the try block like this

app.use(async (ctx, next) => {
  try {
    ctx.set('Access-Control-Allow-Origin', '*')
    await next()
  } catch (err) {
    ctx.set('Access-Control-Allow-Origin', '*')
    ctx.status = err.status
})

Otherwise, there will be no header you set in the normal code.

0reactions
niftylettucecommented, Jan 6, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make koa ctx.throw() use application/json rather than ...
throw () means any stacktrace is emitted to the server logs and also any custom error message is sent in the response. The...
Read more >
Responses | Strapi Documentation
attributes - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used when scheme is a string,...
Read more >
Context - ThinkJS
Helper function to throw error When !value equals true , similar to ... If you don't want to use Koa's buildin response, just...
Read more >
koajs/koa - Gitter
it's my first time using streams, so I'm a bit confused when should I iterate headers/status code? inside on('end') callback?
Read more >
Back to Basics: Custom HTTP Response Header Manipulation ...
HTTP Headers are a core part of the HTTP protocol and while applications rarely need to deal with them, when you need to...
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