In this post, I would like to share how to customize Express server in Sails.js version 0.10.

Handling of the customMiddleware has slightly changed from Sails v0.9.x. In version 0.10 that method needs to be configured in http hook (not express hook, as in the previous version).

It is also very important to remember that your sails.config.http.middleware.order list needs to have $custom middleware entry in it as that will trigger custom middleware function to run.

So in order to add any custom initialization, you can add the following change to the /config/http.js file:

module.exports.http = {
    // ...
    customMiddleware: function(app) {
        // do something ...
    }
    // ...
}

Alternatively, if you would like to perform environment dependent customization, say, in production, you can add following changes to the /config/env/production.js

module.exports = {
    // ...
    http: {
        customMiddleware: function(app) {
            // do something in production environment
        }
    }
    // ...
}

I use that approach to enable trust proxy express flag.

Example:

...
   http: {
    customMiddleware: function(app) {
        app.enable('trust proxy');
    }
  }
...

Code handling can be found on Sails Github: /sails/lib/hooks/http/middleware/load.js.

BTW, when using express hook in Sails 0.10, you will get following warning:

warn: sails.config.express is deprecated; use sails.config.http instead.