Skip to content

Instantly share code, notes, and snippets.

@joshholt
Created January 18, 2012 20:54
Show Gist options
  • Save joshholt/1635589 to your computer and use it in GitHub Desktop.
Save joshholt/1635589 to your computer and use it in GitHub Desktop.
Express Route Middleware
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