Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active April 7, 2018 10:49
Show Gist options
  • Save rjcorwin/0614ed18b3bdb94298e4035e9e62b2f0 to your computer and use it in GitHub Desktop.
Save rjcorwin/0614ed18b3bdb94298e4035e9e62b2f0 to your computer and use it in GitHub Desktop.
/*
Create a folder called `folder-of-code` and then inside of that folder put
other folders like foo and bar with different assets in them. Then run this
server and notice how the static directory served will change depending on
wether you hit `http://foo.lvh.me` and `http://bar.lvh.me`.
Note you could rework this example to change the folder context depending on
something in `req.params.*` but for this example we are using the wildcard
subdomain afforded to us by the *.lvh.me domain which resolves
to 127.0.0.1.
Thanks to `https://github.com/chrisekelley` for laying the groundwork of this
example in `https://github.com/tangerine-community/tangerine/tree/v3`.
*/
const express = require('express');
const path = require('path')
// Set up app.
var app = express();
// Intercept all routes.
app.all('*', function (req, res) {
const folderOfCodeToLookInDeterminedBySubdomain = req.headers.host.split('.')[0]
return express.static(path.join(__dirname, 'folders-of-code', folderOfCodeToLookInDeterminedBySubdomain)).apply(this, arguments);
});
// Listen to on port 80.
app.listen(80, function () {
console.log('Proxy is listening on port 80');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment