Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express detect if request came from a subdomain?

I am using npm subdomain, and to keep routing clean I would like to do something like.

router.get('/login_success',function(req,res) {

  if(req.subdomain) {
    res.redirect('/');
  } else {
    res.sendStatus(200);
  } 

});

In the above, if the user logs in the app via sub-domain, I redirect to the sub-domain root which renders a profile.

If the user logs in via the non sub-domain route, it's using ajax to render a view so I need to send a 200 status saying the user is valid.

I figured this is much more clean that having two separate callback routes for each.

So my question, is there a way to detect if the req came from a sub-domain or regular domain.

I know there is req.headers.origin but I am wondering if there is a more relative way of doing it, so when going back and forth between local and production I don't have to change it.

like image 369
Michael Joseph Aubry Avatar asked Feb 27 '15 21:02

Michael Joseph Aubry


1 Answers

Use req.subdomains, this will give you a list of subdomains used in the url. Here's where I got the information from.

like image 64
MJPinfield Avatar answered Oct 16 '22 15:10

MJPinfield