Skip to content

Instantly share code, notes, and snippets.

@ni-ka
Last active August 29, 2015 14:04
Show Gist options
  • Save ni-ka/095511910d54edcdcc47 to your computer and use it in GitHub Desktop.
Save ni-ka/095511910d54edcdcc47 to your computer and use it in GitHub Desktop.
Custom brunch server to proxy api requests to backend (supports reload)

Usage

  1. Put server.coffee next to config.coffee
  2. Register server.coffee in config.coffee server: path: 'server.coffee'
  3. Add required modules to package.json npm install --save express http-proxy

Notes

  1. Supports reloading environement when bower.json / package.json is changed (returns node server)
express = require 'express'
sysPath = require 'path'
http = require 'http'
httpProxy = require 'http-proxy'
apiProxy = httpProxy.createServer({
target:'http://localhost:3000'
});
exports.startServer = (port, path, callback) ->
app = express();
app.use express.static path
app.all "/api/*", (req, res) ->
apiProxy.web(req, res)
app.all '/*', (request, response) ->
response.sendfile sysPath.resolve sysPath.join path, 'index.html'
server = http.createServer app
server.listen parseInt(port, 10), callback
server
@morkeleb
Copy link

Added a single line that allows usage of external servers for the api.
https://gist.github.com/morkeleb/455199f983557a98f36c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment