express-lazy

1.1.1 • Public • Published

express-lazy

Extends express 4.x with lazy methods, for instant-on servers (particularly on embedded devices).

Instead of loading and processing all of the requires for your apps all at once, you can wait until the route is used and load the middleware (nearly) instantly at that time.

This pattern will produce a server that is ready to handle requests much quicker than traditional means.

  • .lazy(createHandler);
  • .lazy(mountname, createHandler);
  • .lazyMatch(mountname, routePrefix, createHandler);

Usage

'use strict';

var express = require('express-lazy');
var query = require('connect-query');
var app = express();

app.use(query());

// Calls .use(pathname, handler) under the hood
app.lazy('/api/texter', function () {
  var urlrouter = require('urlrouter');
  var mySmsHandler = require('./my-sms-handler');
  return urlrouter(mySmsHandler);
});

// Calls .use('/api', handler) and manually checks that the remaining route prefix matches
app.lazyMatch('/api', '/webhooks/email', function () {
  return require('./my-email-handler').create({ foo: 'bar'}).then(function (mailRestApi) {
    var urlrouter = require('urlrouter');
    return urlrouter(mailRestApi);
  });
});

app.use(express.static(__dirname + '/public'));

return app;

Note that you can return a route handling function or a promise;

Alternate Usage

'use strict';

var express = require('express');
var lazify = require('express-lazy');
var app = express();

lazify.inject(app);

// ...

Package Sidebar

Install

npm i express-lazy

Weekly Downloads

1

Version

1.1.1

License

Apache2

Last publish

Collaborators

  • coolaj86