Skip to content

Instantly share code, notes, and snippets.

@robinmonjo
Created July 3, 2013 22:19
Show Gist options
  • Save robinmonjo/5923355 to your computer and use it in GitHub Desktop.
Save robinmonjo/5923355 to your computer and use it in GitHub Desktop.
Simple static file server in express with authentication
express = require 'express'
fs = require 'fs'
app = express()
authenticationFilter = (req, res, next) ->
auth_token = req.query.auth_token
if auth_token == process.env.AUTH_TOKE
next()
else
res.status(401).send('Forbiden')
app.use authenticationFilter
app.get '/*', (req, res) ->
fileName = req.params
fs.readFile "./static_files/#{fileName}", 'utf8', (err, data) ->
if err
res.status(404).send('Not found')
else
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Content-Length', data.length)
res.end(data)
port = process.env.PORT || 9999
console.log "Listening on port #{port}"
app.listen port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment