req.headers.origin is undefined req.headers.origin is undefined express express

req.headers.origin is undefined


The origin may be hidden if the user comes from an ssl encrypted website.

Also: Some browser extensions remove origin and referer from the http-request headers, and therefore the origin property will be empty.

You might want to create some sort of authentication token and pass it as a parameter, instead on relying on request headers. Especially since the headers can be faked/manipulated.


Try with this:

app.use(function(req, res, next) {  res.header("Access-Control-Allow-Origin", req.header('origin'));  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  res.header("Access-Control-Allow-Credentials","true");  next();}); 


Try this

var host = req.headers.host;

OR

var host = req.get('host');