Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure multiple sub domains in Express.js or Connect.js

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 ?

like image 213
Raks Avatar asked Apr 26 '11 13:04

Raks


People also ask

Can you have multiple sub domains?

Each domain name can have up to 500 subdomains. You can also add multiple levels of subdomains, such as info.blog.yoursite.com. A subdomain can be up to 255 characters long, but if you have multiple levels in your subdomain, each level can only be 63 characters long.

Can localhost have subdomains?

localhost is not supposed to have any subdomains. To do so violates the approved RFC standards. localhost has an A record and in IPv6 environments, an AAAA record. All other DNS record types, including SOA are forbidden.


1 Answers

Or alternatively you could use vhost.

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:

var vhost = require('vhost');  express() .use(vhost('m.mysite.com', require('/path/to/m').app)) .use(vhost('sync.mysite.com', require('/path/to/sync').app)) .listen(80) 

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

like image 148
Adrien Avatar answered Nov 06 '22 02:11

Adrien