Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js vhost subdomain set up

Trying to set up connect's vhost middleware. Would love some help.

I've got my normal express.js app, with node_modules, public, views and routes directories. I've added another directory next to those which holds another express.js app.

I've added this line to my top level app (tedxgramercy):

app.use(express.vhost('chatter.tedxgramercy.com', require('./chatter/app.js').app));

And this line to my chatter app:

var app = exports.app = express();

The chatter app calls listen on port 8000, the main (top level) app calls listen on port 3000. I don't know if that's right.

When I launch my app (node app) it runs fine and I can access both apps on localhost:3000 and localhost:8000 respectively, but when I deploy to my server, the subdomain http://chatter.tedxgramercy.com doesn't work.

Any pointers? Do I have to change my DNS to point to the other port or something?

like image 310
Costa Michailidis Avatar asked Oct 06 '13 19:10

Costa Michailidis


1 Answers

It's a simple, but somewhat tricky setup.

First, the main app.js:

var vhost = require('vhost');

app.use(vhost('chatter.tedxgramercy.com', require('./chatter/app').app))
app.use(router);

I included the router to make it clear that it is critical for it to be used after configuring virtual hosts.

Then, in chatter/app.js:

var express = require('express');
var app = express();
var path = require('path');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

var router = express.Router();

router.get('/', function(req, res, next) {
  res.render('index');
});

app.use(router);

exports.app = app;

This is the bare minimum setup to render a Jade template in a sub app. Notice that app is exported, but no server is actually started since the main app is the server.

like image 143
Pier-Luc Gendreau Avatar answered Nov 05 '22 14:11

Pier-Luc Gendreau