Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to issue 71 "undefined headers.origin" #262

Closed
DaliuSinger opened this issue Feb 4, 2022 · 1 comment
Closed

Solution to issue 71 "undefined headers.origin" #262

DaliuSinger opened this issue Feb 4, 2022 · 1 comment

Comments

@DaliuSinger
Copy link

DaliuSinger commented Feb 4, 2022

I am not sure if this is cors issue or in general express issue however, there was a disscussion about that, so I decided to post my solution.

I was running into problems with getting req.headers.origin from "GET" method while using node.js with "express" router. However, using other methods, like POST req.headers.origin was working as expected.

In the discussion (https://github.com/expressjs/cors/issues/71) mentioned, was a solution to replace missing origin link with host:
req.headers.origin = req.headers.origin || req.headers.host;
However, I realised that referer always includes a full host link. Here is my solution that eventually replaces origin with another link scrambled from refefer.

app.use(function(req,res,next){
  if(!req.headers.origin) {
    if(req.headers.referer) {
      const url = new URL(req.headers.referer);
      req.headers.origin = url.origin;
    }
  }
  next();
});

What this does is scramble link from referer if original headers.origin is undefined and replaces correct link to headers.origin.
Hope this helps anyone who still has issues with this problem.

@dougwilson
Copy link
Contributor

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants