6

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?

James
  • 5,137
  • 5
  • 40
  • 80
  • What's in, say, `./lib/subdomains/api`? – robertklep Apr 03 '13 at 12:27
  • Something along the lines of [this Gist](https://gist.github.com/jbrooksuk/5300772) the thing I was trying was to do at one point was `app.use(express.vhost('api.example.com', require('./lib/subdomains/api').app))` – James Apr 03 '13 at 12:32
  • Here's a [gist](https://gist.github.com/robertklep/5300792) that works just fine for me. Are you checking in a browser or with a CLI tool? If the latter, is it sending the correct `Host` header? – robertklep Apr 03 '13 at 12:39
  • In the browser. Your code is the same as what I have, except for that I have beneath my `app.configure` this `require('./lib/routes')(app);` Is it possible that the `app` is clashing? – James Apr 03 '13 at 12:44
  • Could `/var/vhosts` be an Apache-thing? I just realised that usually, to create new host entries, you edit `/etc/hosts`. The `app`'s shouldn't be clashing, Node has a module scope so `app` in one module is separate from `app` in another module. – robertklep Apr 03 '13 at 12:50
  • Yeah it is, Apache isn't installed mind, but httpd is. Thing is, I'm not sure if this is required. – James Apr 03 '13 at 12:51
  • Well if you don't get any errors trying to open `api.example.com` your browser can resolve it just fine, so it's probably not the reason for your problem :) – robertklep Apr 03 '13 at 12:52
  • I have done, but it resolves to the main site routes. – James Apr 03 '13 at 12:53
  • Could you post your routing/middleware setup? Including `app.configure` and stuff. Perhaps there's something going on there. – robertklep Apr 03 '13 at 12:54
  • [This](https://gist.github.com/jbrooksuk/5300966) should be everything you need. – James Apr 03 '13 at 12:55
  • The only way I see this failing is if you declare any routes *before* your `app.configure`. Otherwise, it looks fine (and works okay for me). – robertklep Apr 03 '13 at 13:01
  • Bizarre. Does it work for you if you include the `api` routes in a separate file? Here is a cut down version of my `app.js`. https://gist.github.com/jbrooksuk/5301027 – James Apr 03 '13 at 13:03
  • I've got it working! I had `req` and `res` params the wrong way round in my `api.js` module and it was causing crashes. – James Apr 03 '13 at 13:08

1 Answers1

9

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 :)

Arash Milani
  • 6,149
  • 2
  • 41
  • 47
James
  • 5,137
  • 5
  • 40
  • 80