Does Express detect if a request comes from a subdomain?

I use the npm subdomain and 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 example, if a user logs into the application through a subdomain, I redirect him to the root subdomain that displays the profile.

If a user logs in via a non-subdomain route, he uses ajax to render the view, so I need to send a status of 200, saying that the user is valid.

I figured it was much cleaner that there are two separate callback routes for each.

So my question is, is there a way to determine if req came from a subdomain or a regular domain.

I know that there is req.headers.origin , but I wonder if there is a more relative way to do this, so when moving between the local and the production process, I do not need to change it.

+6
source share
1 answer

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

+11
source

Source: https://habr.com/ru/post/983128/


All Articles