Created
October 29, 2012 18:47
-
-
Save icholy/3975644 to your computer and use it in GitHub Desktop.
brunch server with proxy support
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' | |
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, config) -> | |
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 |
Check https://gist.github.com/ni-ka/095511910d54edcdcc47 for an updated version which supports watcher reload (when changing bower.json / package.json)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do i use this? :)