Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Vadim was <a href="https://stackoverflow.com/questions/11960705/expressjs-server-how-to-handle-multiple-domains#comment15940267_11960962">almost onto the right idea</a>. You can configure how to respond to each domain with the <a href="http://www.senchalabs.org/connect/vhost.html" rel="noreferrer"><code>vhost</code> middleware</a>:</p> <pre><code>// `baz.com` var app = express.createServer(); app.get( '/', routes.index ); // ... express.createServer() .use( express.vhost( 'foo.com', express.static( '/var/www/foo' ) ) ) .use( express.vhost( 'bar.net', express.static( '/var/www/bar' ) ) ) .use( express.vhost( 'baz.com', app ) ) .use( function( req, res ) { res.send('Sorry, I do not know how to handle that domain.'); }) .listen( ... ); </code></pre> <p><code>routes.index</code> can then be simplified to handle only <code>baz.com</code> requests:</p> <pre><code>exports.index = function( req, res ) { // ... lines ... res.render( 'index', { title: 'Baz Title example' } ); }; </code></pre> <p><strong>Edit</strong></p> <p>As for comparisons:</p> <p>The <code>switch</code> would effectively be done first and would determine how to handle all requests based on the <code>host</code> -- similar to:</p> <pre><code>express.createServer().use(function( req, res, next ) { switch( req.host ) { case 'foo.com': express.static( '/var/www/foo' )( req, res, next ); break; case 'bar.net': express.static( '/var/www/bar' )( req, res, next ); break; case 'baz.com': app.handle( req, res, next ); break; default: res.send( ... ); } }).listen( ... ); </code></pre> <p>It allows you to set the stack on start so any middleware is available immediately:</p> <pre><code>server.stack = [ express.vhost( 'foo.com', ... ), express.vhost( 'bar.net', ... ), express.vhost( 'baz.com', ... ), [Function] ]; </code></pre> <p>These also reflect the 2 possible sources of issues you might have:</p> <p><strong>Same stack without filters</strong></p> <p>Each <code>Application</code> only has 1 middleware stack, which all of the middleware you're using is being added directly to with <code>app.use(...)</code>. Despite adding some under conditions, you're still getting:</p> <pre><code>app.stack = [ // ..., app.router, express.static( '/var/www/foo' ), express.static( '/var/www/bar' ) ]; </code></pre> <p>And the condition won't change how the <code>static</code> middlewares respond -- which is by <code>req.path</code>, not <code>req.host</code> -- only when they're in the stack to start responding.</p> <p><strong>State of the stack</strong></p> <p>And, if the <code>static</code> middlewares aren't added until <em>after</em> another request has been made, then I take it they aren't available immediately:</p> <pre><code>// GET http://foo.com/file 404 app.stack = [ app.router ] // GET http://foo.com/ 200 app.stack = [ app.router, express.static( '/var/www/foo' ) ] // GET http://foo.com/file 200 app.stack = [ app.router, express.static( '/var/www/foo' ) ] </code></pre> <p>This may also mean the same <code>static</code> middleware could be added to the stack multiple times:</p> <pre><code>// 3x GET http://foo.com/ app.stack = [ app.router, express.static( '/var/www/foo' ), express.static( '/var/www/foo' ), express.static( '/var/www/foo' ) ] </code></pre> <p>And having their addition depend on other requests also suggests a possible race conditions:</p> <pre><code>// was `foo.com` or `bar.net` first? app.stack = [ app.router, express.static( ? ), express.static( ? ) ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload