|
# Create a middleware to detect a mobile browser and attach a boolean to the request |
|
# Create express app that servers links to images using staticProvider |
|
# Modify profiler to profile your app and write profile data to a log file |
|
# Create a middleware factory that sets the HTTP expires header based on roles |
|
express = require "express" |
|
fs = require "fs" |
|
|
|
app = express.createServer() |
|
|
|
browserDetector = (req, res, next) -> |
|
req.googled = false |
|
if req.headers['user-agent'].indexOf("Chromium") isnt -1 |
|
req.googled = true |
|
next() |
|
|
|
mFactory = (role) -> |
|
expires = "Thu, 15 Apr 2010 20:00:00 GMT" |
|
if role is "permanent" |
|
expires = "Thu, 15 Apr 2150 20:00:00 GMT" |
|
(req, res, next) -> |
|
res.header "Expires", expires |
|
next() |
|
|
|
# True monkeypatching, override console.log to capture Profiler output |
|
originalLogger = console.log |
|
logFile = fs.createWriteStream "profile.log" |
|
console.log = (logVars...) -> |
|
originalLogger logVars... |
|
logVars.forEach (logVar) -> |
|
logFile.write "#{logVar}\n" |
|
|
|
app.use express.profiler() |
|
app.use browserDetector |
|
app.use "/images", express.static "#{process.cwd()}/images" |
|
|
|
app.get "/permanent", mFactory("permanent"), (req, res, next) -> |
|
res.end "This page never expires in your lifetime" |
|
|
|
app.get "/", (req, res, next) -> |
|
res.contentType "text/html" |
|
res.write "<img src='/images/viking.jpg' />" |
|
if req.googled |
|
return res.end "You're using a Chromium" |
|
res.end "You're a browser" |
|
|
|
app.listen 8003 |
The Qaiku thread that belongs to this gist: http://www.qaiku.com/channels/show/seminaarikannu/view/e8ecfcde811d11e0aa009badba40ad8cad8c/