I have trouble using the express middleware on sails 0.11.0. I tried the following in http.js file.
module.exports.http = {
customMiddleware: function (app) {
console.log("middleware");
app.use('/test', express.static('****/****/*****/testProject/api/controllers' + '/public/'));
}
};
But it doesn't work, I am missing something.
This worked for me in Sails 0.12. Modified the config/http.js and added the following:
Allowed me to do SPP without any engine nor use another http server to serve the static content.
EDIT: Sails change since i wrote this answer, please, look if @Meeker answer works.
** OLD ANSWER **
This work for me:
With the latest Sails (0.12), calling express-style
app.use(...)
isn't directly possible fromconfig/http.js
(or as a policy) because the custom middleware function parameters changed fromfunction (app)
tofunction (req, res, next)
. This is probably why some of the answers here don't work with Sails 0.12.This change isn't clearly mentioned in Sails documentation and migration guides, but looking at Express documentation, we see that
app.use(...)
can accept a singlefunction (req, res, next)
argument... so my guess was to assign the parameter toapp.use(...)
as a custom middleware inconfig/http.js
like so (using connect-history-api-fallback as an example):And voilà, it works like a charm!
TL;DR Since Sails 0.12, custom middlewares defined in
config/http.js
should be of the typefunction (req, res, next)
and notfunction (app)
, so instead of callingapp.use(someMiddleware)
, you should placesomeMiddleware
directly insails.config.http.middleware.order
.On sails 0.12.13
function (app)
still works, just pay attention where you place the middleware code (config/http.js):After doing this you should be able to access the password file in your system using this URL: http://localhost:1337/public/passwd
Your syntax is a little off and you need to return the execution from the provided call back. It should look just like a controller.action.
I'm including the example from docs, this should help you