Take the 2-minute tour ×

I am used to working on httpd ( Apache ) which provides a way to configure subdomains which is mapped to a directory. How can I do the same thing in Connect.js/Express.js ? I see that the only thing that I have is routes which I am not sure how I can use to configure sub domains. I have subdomains like m.mysite.com, sync.mysite.com

Can someone help ?

share|improve this question
The accepted answer should be updated as the alternative answer is a much better solution to this problem. –  Brian Wigginton Nov 4 '11 at 4:30
add comment

3 Answers

up vote 55 down vote accepted

Or alternatively you could use connect.vhost (which is available as express.vhost in express).

Then, create several sites in their own directory and export the express app, eg. /path/to/m/index.js:

var app = express()
/* whatever configuration code */
exports.app = app
// There is no need for .listen()

And then handle all requests with the following app:

.use(express.vhost('m.mysite.com', require('/path/to/m').app))
.use(express.vhost('sync.mysite.com', require('/path/to/sync').app))

Note that /path/to/m and /path/to/sync can be absolute paths (as written above) or relative paths.

share|improve this answer
This seems like a much cleaner and clearer way of achieving sub domains with express. –  btleffler Oct 25 '11 at 18:02
And then what should be in /path/to/m and /path/to/sync? Another express app, as usual? –  Costa Jun 7 '13 at 17:20
Yes, @Costa. Each vhost mapping should point to an express app. –  countfloortiles Jul 26 '13 at 18:05
@countfloortiles Thanks! I've set this up, but it won't let me deploy to nodejitsu, claiming that the subdomain's express object is not a function. Weird error. Any ideas? –  Costa Oct 7 '13 at 15:55
@costa make sure youre using modules.export to expose the object –  qodeninja Feb 4 at 18:17
show 1 more comment

You could append a subdomain to a request and then check for it in subsequent next() calls.

I got the following code from > http://groups.google.com/group/express-js/browse_thread/thread/b04bbaea7f0e8eed (so full credit to the original author)

  if(req.headers.host == 'some.sub.domain.com')  //if it's a sub-domain
    req.url = '/mysubdomain' + req.url;  //append some text yourself

This will mean that all get requests that come from the subdomain will get 
/subomdin appended to them, so then you can have routes like this 
.get('/blogposts', function(){ 
  // for non-subdomain 

.get('/mysubdomain/blogposts', function(){ 
   // for subdomain 
share|improve this answer
add comment

I created a module to help with subdomains in Express: https://github.com/WilsonPage/express-subdomain-handler

share|improve this answer
Hi, your subdomain-handler looks interesting. How would I use this with say "dev.localhost"? What else would I need to change besides "baseUrl:localhost", it's not very clear from the example you posted. Thanks. –  braitsch May 20 '12 at 4:35
If you are using it locally you will have to make sure that the subdomain you are using is listed inside your machine's 'hosts' file. When in production you can setup wildcard subdomains so that you dont have to list every subdomain you need. I usually use the format: subdomain.mysite.dev (locally) and subdomain.mysite.com (in production). –  wilsonpage May 20 '12 at 10:13
add comment

Your Answer


