Skip to content

Instantly share code, notes, and snippets.

@icholy
Created October 29, 2012 18:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/3975644 to your computer and use it in GitHub Desktop.
Save icholy/3975644 to your computer and use it in GitHub Desktop.
brunch server with proxy support
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
@andriijas
Copy link

How do i use this? :)

@ni-ka
Copy link

ni-ka commented Jul 17, 2014

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