Middleware

asimov-server accepts any Express-compatible middleware. The contract is the familiar three-argument signature:

function (req, res, next) { /* ... */ }

You register middleware from a plugin, either by pushing it directly onto the server or by adding it to a named sequence.

Mounting middleware

lib/middleware/requestId.js
var crypto = require('crypto');
module.exports = function requestId (req, res, next) {
req.id = crypto.randomBytes(8).toString('hex');
res.setHeader('x-request-id', req.id);
next();
};
// index.js
var asimov = require('asimov');
var server = require('asimov-server');
var requestId = require('./lib/middleware/requestId');
module.exports = function plugin () {
asimov.use(server);
asimov.middleware(requestId);
};

Path-scoped middleware

Pass a path as the first argument to scope:

asimov.middleware('/api', someJsonHandler);

Sequences as middleware

Middleware is implemented on top of asimov.js’s sequences. You can drop into the underlying sequence API to compose stages that aren’t request-shaped:

asimov.addSequence('render');
asimov.render(function (page, next) {
page.html = renderMarkdown(page.body);
next();
});
asimov.runSequence('render', somePage)
.done(function () { /* ship it */ })
.fail(function (err) { /* log it */ });

Third-party middleware

Anything that works in Express works here. body-parser, helmet, compression, serve-static, morgan — all drop in unmodified.