sailsjs v0.10 express customMiddleware not loading
Handling of the customMiddleware
has slightly changed in Sails 0.10. In version 0.10 that method needs to be configured in http
hook (not express
hook, as in 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; usesails.config.http
instead.
You have to specify an additional config for config.express.costumMiddleware
to be mounted. By setting config.middleware.custom
to true
you enable this default behavior of previous Sails versions.
// config/express.jsmodule.exports.express = { middleware: { custom: true }, customMiddleware: function(app){ // express middleware here }};
Related commit
Related source