Extends express with a .lazy(pathname, createHandler), analogous to .use(pathname, handler), for instant-on servers.

Usage no npm install needed!

<script type="module">
  import expressLazy from '';



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);


'use strict';

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


// 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();


// ...