Zach Case
Zach Case

Reputation: 13

How Can I Use vhost to Redirect a Sub-Domain to an ExpressJS Route?

How can I use ExpressJS' vhost middleware to point a sub-domain (business.domain.com) to a defined route (domain.com/business)?

Edit: lukaszfiszer's suggestion worked and points to the correct router now, but it doesn't load any assets. So it is just a blank HTML page from the jade template. The main page works fine, it is just when you access the subdomain via vhost that the assets give a 404.


You can view the main site here: http://ddbuddyapp.com

You can view the subdomain here: http://business.ddbuddyapp.com


Edit: Below is the app.js that fixed everything and had the assets working correctly

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var vhost = require('vhost');

var main = require('./routes/index');
var business = require('./routes/business');
var government = require('./routes/government');
var university = require('./routes/university');
var community = require('./routes/community');

var app = express();

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

// uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

// Apply Vhost
app.use(vhost('business.localhost', function(req, res, next) {
    req.url = "/business" + req.url;
    next();
}));

app.use('/', main);
app.use('/business', business);
app.use('/government', government);
app.use('/university', university);
app.use('/community', community);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
// if (app.get('env') === 'development') {
//     app.use(function(err, req, res, next) {
//         res.status(err.status || 500);
//         res.render('error', {
//             message: err.message,
//             error: err
//         });
//     });
// }

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

Upvotes: 1

Views: 1563

Answers (1)

lukaszfiszer
lukaszfiszer

Reputation: 2631

Instead of passing a subapplication as a second argument, pass it a function that will transform the url.

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

app.use(vhost('*.domain.com', function(req, res, next) {
    req.url = req.url + req.vhost[0];
    next();
}));

(...)

app.get('/business', function(req, res) {
    res.send('request to business.domain.com received!');
});

Upvotes: 1

Related Questions