Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local subdomains using express.js 's req.subdomains?

Here is my /etc/hosts file

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
127.0.0.1   test.localhost
127.0.0.1   demo.localhost
127.0.0.1   test2.localhost
255.255.255.255 broadcasthost
::1             localhost 

All I need is to be able to parse out the subdomain from the url as I would have to store it in the session cookie for session use (for now). I'm not trying to do anything fancy - like set up apis or anything as of now.

Express's documentation shows that there is a req.subdomains property that lists the subdomains as an array, but with all of my local subdomains, this array shows up empty. [ i.e. when i go to demo.localhost:3000, the web app is served for sure, but on the first get function, I call req.subdomains and there is an empty array [].

How can I get these subdomain strings from my localhost:3000?

like image 218
Darshan Avatar asked Mar 19 '15 13:03

Darshan


1 Answers

This is because your host is missing a TLD. From the Express code -- subdomain offset will default to 2 because it expects something like .com rather than for the host to end with localhost.

You could use this.app.set('subdomain offset', 1); as part of the configuration for running locally.

like image 70
Explosion Pills Avatar answered Oct 13 '22 01:10

Explosion Pills