Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Configuration for user-provided middlewares #661

wants to merge 2 commits into from

2 participants


Now the developer can load custom middlewares from a folder. If options.midDir is empty, the folder ./middleware is assumed to be the default location to store custom middlewares. This new configuration is useful for middlewares that need to be loaded before the server starts.

Example of configuration:


options.midDir = 'middleware';
// options.midDir = './custom-scripts/middleware'; works too

// Some middlewares
options.appConfig = {
middleware: [

var app = Mojito.createServer(options);



The change you are proposing is not backwards compatible.

It seems that the following behaviour needs to be documented better:

if you use 'middleware/passport-init' instead of 'middleware-passport-init', you can specify any subfolder transparently and mojito will handle it correctly.

Also, if you name one of your middleware files beginning with mojito, e.g. 'middleware/mojito-my-Y-enabled-script' you will disable the default mojito middleware and your script will be passed a config object.

2c Ron


it's just another way to set up where to load middlewares. If options.midDir was not specified, mojito will assume the folder 'middleware' as default. But you can still set up application.json.


@ooskapenaar hmm... It would be better to let application.json handle all of this.

@rodnic rodnic closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 1 deletion.
  1. +7 −1 lib/mojito.js
8 lib/mojito.js
@@ -199,6 +199,11 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
if (!options.context) {
options.context = {};
+ if (!options.midDir) {
+ options.midDir = 'middleware';
+ }
// all logging that comes from YUI comes from here
// We need to do this early, since creating a Y instance appears to copy
@@ -394,7 +399,7 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
} else {
// backwards-compatibility: user-provided middleware is
// specified by path
- midPath = libpath.join(options.dir, midName);
+ midPath = libpath.join(options.dir, options.midDir, midName);
//console.log("======== MIDDLEWARE user " + midPath);
midBase = libpath.basename(midPath);
if (0 === midBase.indexOf('mojito-')) {
@@ -404,6 +409,7 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
midFactory = require(midPath);
} else {
+ // Load user-provided middleware from middleware folder
Something went wrong with that request. Please try again.