Created
July 3, 2013 22:19
-
-
Save robinmonjo/5923355 to your computer and use it in GitHub Desktop.
Simple static file server in express with authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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