Last active
December 15, 2015 02:58
-
-
Save icholy/5190534 to your computer and use it in GitHub Desktop.
This file contains 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
express = require 'express' | |
httpProxy = require 'http-proxy' | |
sysPath = require 'path' | |
config = require('./config').config | |
startExpress = (port, base, path, callback) -> | |
server = express() | |
server.use (request, response, next) -> | |
response.header 'Cache-Control', 'no-cache' | |
next() | |
server.use base, express.static path | |
server.all "#{base}/*", (request, response) -> | |
response.sendfile sysPath.join path, 'index.html' | |
server.listen port, callback | |
server | |
startProxy = (port, routes, defaultRoute, callback) -> | |
server = httpProxy.createServer (req, res, proxy) -> | |
url = req.url | |
target = null | |
for route in routes | |
if url.match(route.re) isnt null | |
target = route | |
break | |
target = defaultRoute if not target? | |
proxy.proxyRequest req, res, {host: target.host, port: target.port} | |
server.listen port, callback | |
server | |
exports.startServer = (port, publicPath, callback) -> | |
proxyPort = port | |
expressPort = port + 1 | |
routes = config.server.routes || {} | |
defaultRoute = | |
host: '127.0.0.1' | |
port: expressPort | |
startExpress expressPort, config.server.base, publicPath, -> | |
startProxy proxyPort, routes, defaultRoute, callback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment