Skip to content

Instantly share code, notes, and snippets.

@todoubled
Created December 17, 2012 01:58
Show Gist options
  • Save todoubled/4315262 to your computer and use it in GitHub Desktop.
Save todoubled/4315262 to your computer and use it in GitHub Desktop.
Mock server for local development
#
# Use a fixture stub of JSON to mock a headline API to respond to GET /headline
#
fs = require 'fs'
express = require 'express'
root = require('path').normalize "#{__dirname}/.."
pub = "#{root}/app/public"
headline = JSON.parse fs.readFileSync "#{root}/app/fixtures/headline-api.json", 'utf8'
module.exports = routes = express()
# ---
routes.get '/headline', (req, res) ->
res.send headline
#
# ## Server
#
# ###### Mock API endpoints and serve static assets in `app/public`.
#
# ---
#
# - Define custom routes with `routesPath` for API endpoints.
# - Mock edge-case result sets for fast, offline integration tests.
#
fs = require 'fs'
express = require 'express'
root = require('path').normalize "#{__dirname}/.."
routesPath = "routes.coffee"
port = process.env.PORT || 8080
app = express()
app.use app.router
app.use express.static pub
app.use express.bodyParser()
# Use any custom routes if they're defined.
app.use require(routesPath) if fs.existsSync routesPath
# Allow the integration test runner to run at a different port to avoid collision with `make server` on `8080`.
app.listen port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment