diff --git a/lib/modules/http.js b/lib/modules/http.js index 2277903..5c6247a 100644 --- a/lib/modules/http.js +++ b/lib/modules/http.js @@ -1,7 +1,8 @@ 'use strict'; module.exports.create = function (deps, conf) { - var app = require('express')(); + var express = require('express'); + var app = express(); var adminApp = require('./admin').create(deps, conf); var domainMatches = require('../match-domain').match; @@ -90,6 +91,38 @@ module.exports.create = function (deps, conf) { }; } + function createStaticRoute(mod) { + var getStaticApp, staticApp; + if (/:hostname/.test(mod.root)) { + staticApp = {}; + getStaticApp = function (hostname) { + if (!staticApp[hostname]) { + staticApp[hostname] = express.static(mod.root.replace(':hostname', hostname)); + } + return staticApp[hostname]; + }; + } + else { + staticApp = express.static(mod.root); + getStaticApp = function () { + return staticApp; + }; + } + + return function (req, res, next) { + var hostname = req.headers.host.split(':')[0]; + var relevant = mod.domains.some(function (pattern) { + return domainMatches(pattern, hostname); + }); + + if (relevant) { + getStaticApp(hostname)(req, res, next); + } else { + next(); + } + }; + } + app.use(redirectHttps); app.use(handleAdmin); @@ -97,6 +130,9 @@ module.exports.create = function (deps, conf) { if (mod.name === 'proxy') { app.use(createProxyRoute(mod)); } + else if (mod.name === 'static') { + app.use(createStaticRoute(mod)); + } else { console.warn('unknown HTTP module', mod); }