Browse files

Simplification: middleware don't need wrapping.

Their `next()` callback is already a regular Node-style callback; it already
works with Streamline syntax just fine.
  • Loading branch information...
1 parent 85ece04 commit 4b2111754c80c8bd98efa372f3404fe94f24da1f @aseemk committed Jun 24, 2012
Showing with 7 additions and 26 deletions.
  1. +5 −16
  2. +2 −10 index.js
@@ -1,32 +1,21 @@
module.exports = express = require 'express'
# wraps the streamline-style handler (req, res, _) to the style express
-# requires (req, res, next). we have different methods for middleware vs.
-# route/resource handlers to account for the different completion semantics.
-# update: account for error handlers which have the form (err, req, res, _)!
-wrapHandler = (handler, isMiddleware) ->
+# requires (req, res, next). note that error handlers have an extra parameter,
+# and express examines the route function's length to determine this!
+wrapHandler = (handler) ->
makeCallback = (next) ->
(err) ->
- if err
- next(err)
- else if isMiddleware
- next()
+ return next err if err
if handler.length >= 4
(err, req, res, next) ->
handler err, req, res, makeCallback next
(req, res, next) ->
handler req, res, makeCallback next
-# we wrap the Express server's instance methods via its prototype:
+# we monkey-patch the route and error methods via Express's prototype::
app = express.HTTPServer.prototype
-# we monkey-patch the middleware method here:
-origAppUse = app.use
-app.use = (middleware) ->
- @, wrapHandler middleware, true
-# and we monkey-patch the route and error methods here:
for verb in ['all', 'get', 'post', 'put', 'del', 'error']
do (verb) ->
origAppVerb = app[verb]

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 4b21117

Please sign in to comment.