Created
January 18, 2012 20:54
-
-
Save joshholt/1635589 to your computer and use it in GitHub Desktop.
Express Route Middleware
This file contains hidden or 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
var findComponent = function(service_doc, req) { | |
return service_doc.components.filter(function (c) { | |
var c_fixed = c.name.toLowerCase().replace(/\s*/g, ''), | |
req_fixed = req.params.component.toLowerCase(); | |
return c_fixed === req_fixed; | |
}); | |
}; | |
module.exports = { | |
configureInstance: function(service_doc) { | |
return function(req, res, next) { | |
if (!req.params.component) return next(new Error("We cannot configure an unknown component...")); | |
var found = findComponent(service_doc, req); | |
if (found.length <= 0) return next(new Error("We cannot configure an unknown component...")); | |
req.title = found[0].name; | |
next(); | |
}; | |
}, | |
previewInstance: function(service_doc) { | |
return function(req, res, next) { | |
if (!req.params.component) return next(new Error("We cannot preview an unknown component...")); | |
var found = findComponent(service_doc, req); | |
if (found.length <= 0) return next(new Error("We cannot preview an unknown component...")); | |
req.result = found[0].componentImage; | |
next(); | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment