Skip to content

Instantly share code, notes, and snippets.

@anderssonjohan
Forked from morkeleb/README.md
Last active August 29, 2015 14:18
Show Gist options
  • Save anderssonjohan/5e33cc2ee586be5b0aa3 to your computer and use it in GitHub Desktop.
Save anderssonjohan/5e33cc2ee586be5b0aa3 to your computer and use it in GitHub Desktop.

Usage

  1. Put server.coffee and fake-api.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)
  2. Edit fake-api.coffee to override responses with i.e. an updated/mockup JSON response that aren't yet available from the proxy target.
exports.setupRoutes = (app) ->
# Sample route
app.all "/api/_fakeApi", (req, res) ->
res.json( {fake:true} )
express = require 'express'
sysPath = require 'path'
http = require 'http'
httpProxy = require 'http-proxy'
fakeApi = require './fake-api'
apiProxy = httpProxy.createServer({
target:'http://localhost:3000'
});
exports.startServer = (port, path, callback) ->
app = express();
app.use express.static path
fakeApi.setupRoutes app
app.all "/api/*", (req, res) ->
delete req.headers.host;
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment