Add a middleware to a route
A middleware is a function that runs before the route handler. Call next() to continue or answer the request yourself to stop it. Register it once with MIDDLEWARE(name, fn) and attach it to a route with #name.
MIDDLEWARE('apikey', function($, next) {
if ($.headers['x-api-key'] === 'abc123')
next();
else
$.invalid(401);
});exports.install = function() {
ROUTE('GET /secure/ #apikey', secure);
ROUTE('GET /open/', open);
};
function secure($) {
$.json({ ok: true });
}
function open($) {
$.json({ ok: true });
}