Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Configuration for user-provided middlewares #661

Closed
wants to merge 2 commits into from

2 participants

@rodnic

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:

#server.js

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

// Some middlewares
options.appConfig = {
middleware: [
'middleware-passport-init',
'middleware-passport-session',
'middleware-facebook-auth'
]
};

var app = Mojito.createServer(options);

@ooskapenaar

-1

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

@rodnic

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.

@rodnic

@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
View
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);
app.use(midFactory(midConfig));
} else {
+ // Load user-provided middleware from middleware folder
app.use(require(midPath));
}
}
Something went wrong with that request. Please try again.