Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js subdomains

I'm trying to get my Node.js powered site to run on one instance serving multiple domains. I have my main domain, example.com and then I have admin.example.com and api.example.com which all have different routes etc, I'm using Express.

So far I've added two A records for the subdomains, and also added two entries into /var/vhosts on my CentOS box.

127.0.0.1 api.example.com
127.0.0.1 admin.example.com
127.0.0.1 example.com

I'm aware that Express has a express.vhost method so I've already tried:

app.use(express.vhost('api.example.com', require('./lib/subdomains/api')))
app.use(express.vhost('admin.example.com', require('./lib/subdomains/admin')))

But that still only serves my main routes which is imported below. What am I missing?

like image 651
James Avatar asked Apr 03 '13 12:04

James


People also ask

Can you have 2 subdomains?

You create subdomains to help organize and navigate to different sections of your main website. Within your main domain, you can have as many subdomains as necessary to get to all of the different pages of your website.

When should you use subdomains?

A subdomain is a division or alias of your domain that can be used to organize your existing website into a separate site. Typically, subdomains are used if there is content that is distinct from the rest of the site. Subdomains are indicated by the section to the left of the root URL.

What is the purpose of subdomains?

A subdomain is a prefix added to a domain name to separate a section of your website. Site owners primarily use subdomains to manage extensive sections that require their own content hierarchy, such as online stores, blogs or support platforms. Subdomains function as a separate website from its domain.

How many subdomains are allowed in a URL?

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.


1 Answers

If anybody else finds this question, you might want to check that you're passing your vhost route parameters the right way around.

I was using:

app.get('/', function(res, req) { /* Do stuff.. */ }

When it should be. The first argument for the callback function is req, then the second one is res.

app.get('/', function(req, res) { /* Do stuff.. */ }

Be diligent with your code :)

like image 167
James Avatar answered Oct 05 '22 16:10

James