Skip to content

Instantly share code, notes, and snippets.

@ghostbar
Created July 23, 2013 07:36
Show Gist options
  • Save ghostbar/6060533 to your computer and use it in GitHub Desktop.
Save ghostbar/6060533 to your computer and use it in GitHub Desktop.
This is the basic "static" server I use in most of my projects. Is using express, with 3 development stages and HTTP Basic Auth for staging. It supports pushState address when it cannot find an static file to match the request. It's supported by Heroku as well. If using with Heroku then make sure to create the NODE_ENV variable for the stage wan…
express = require "express"
app = express()
console.log app.settings.env
oneDay = 86400000
pushState = (req, res) ->
newUrl = req.protocol + '://' + req.get('Host') + '/#' + req.url
res.redirect newUrl
app.configure 'development', () ->
app.use express.compress()
app.use express.static __dirname + '/dist'
app.use express.logger()
app.configure 'staging', () ->
app.use(express.basicAuth('whateveruser', 'whateverpassword'))
app.use express.compress()
app.use express.static __dirname + '/dist'
app.configure 'production', () ->
app.use express.compress()
app.use express.static __dirname + '/dist', { maxAge: oneDay }
app.get '*', (req, res) ->
newUrl = req.protocol + '://' + req.get('Host') + '/#' + req.url
res.redirect newUrl
exports.startServer = (port, path, callback) ->
p = process.env.PORT || port
console.log "Starting server on port: #{p}, path /#{path}"
app.listen p
if callback?
callback app
if process.env.PORT
this.startServer(process.env.PORT, "dist")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment