Last active
December 20, 2015 11:19
-
-
Save shoemaker/6122181 to your computer and use it in GitHub Desktop.
An example "router" for a web server hosting several Node.js apps. In this scenario. I run each application on a different port, using pm2 to keep the process running and watching for changes. The router determines the desired application based on the URL, and then forwards the request to the correct app/port. Static files are served up using Ex…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/nodejitsu/node-http-proxy | |
// https://github.com/visionmedia/express | |
var httpProxy = require('http-proxy'), | |
express = require('express'), | |
app = express(); | |
var PATH_REGEX = /^\/([a-zA-Z0-9]+).*$/; | |
// Create a proxy server with custom application logic. | |
var server = httpProxy.createServer(function (req, res, proxy) { | |
// Grab the first 'directory' from the URL path. | |
var pathParts = req.url.match(PATH_REGEX); | |
// Check if we have something. This identifies the application we should route to. | |
var app = ''; | |
if (pathParts && pathParts.length > 0) app = pathParts[1]; | |
// Determine where to send this request. | |
switch (app.toLowerCase()) { | |
case 'echo' : // Echo app | |
proxy.proxyRequest(req, res, { | |
host: '127.0.0.1', | |
port: 8081 | |
}); | |
break; | |
case 'app1' : // Application with path /app1 | |
proxy.proxyRequest(req, res, { | |
host: '127.0.0.1', | |
port: 8082 | |
}); | |
break; | |
case 'app2' : // Application with path /app2 | |
proxy.proxyRequest(req, res, { | |
host: '127.0.0.1', | |
port: 8083 | |
}); | |
break; | |
// Static files are served up using express. | |
// Paths are defined at the bottom. | |
case 'slides' : // Static files: presentations/slides | |
case 'sandbox' : // All sandbox apps/projects | |
case 'foo' : // Static files: details here | |
proxy.proxyRequest(req, res, { | |
host: '127.0.0.1', | |
port: 8100 | |
}); | |
break; | |
default: // Homepage/root | |
proxy.proxyRequest(req, res, { | |
host: '127.0.0.1', | |
port: 8086 | |
}); | |
break; | |
} | |
}); | |
// Handler to route web socket traffic. | |
// Only needed if you have an application using web sockets. | |
server.on('upgrade', function (req, socket, head) { | |
var pathParts = req.url.match(PATH_REGEX); | |
var app = ''; | |
if (pathParts && pathParts.length > 0) app = pathParts[1]; | |
switch (app.toLowerCase()) { | |
case 'app2': | |
server.proxy.proxyWebSocketRequest(req, socket, head, { | |
host: '127.0.0.1', | |
port: 8083 | |
}); | |
break; | |
} | |
}); | |
server.listen(8000); // Fire up the server, on port 8000 | |
// Define paths for serving up static content. | |
app.use('/slides', express.static(__dirname + '/slides')); | |
app.use('/sandbox', express.static(__dirname + '/sandbox')); | |
app.listen(8100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment