How does one register middleware with app.use as explained in routes.js?
I'd be happy to help with getting this in the documentation if it could be explained to me.
You can access to the express app this way:
var app = require('sails').express.app;
app.use(...);
Thank you for your reply, but that's not working for me.
require('sails').express is undefined.
Yes, because the routes already set during upstart of the environment.
You have to use a hook for that, it can be injected by using customMiddleware which would be bind in
https://github.com/balderdashy/sails/blob/master/lib/express/index.js#L228
For example, you can place something like this in your local.js, e.g.:
express: {
customMiddleware: function (app) {
console.log("config of Middleware is called")
app.use(function (req, res, next) {
console.log("installed customMiddleware is used")
next()
})
}
}
Or you can use policies to achieve the same effect, as I noted in the Stack Overflow question.
As @sgress454 answered in the stackoverflow thread: http://stackoverflow.com/a/18562458/909625 this is where we use policies.
Policies are custom express middleware that use the function(req, res, next) signature and can be applied to all or only some routes. They are standalone modules that live in api/policies and are configured in config/policies.js.
If you need access to the raw express object @TomFreudenberg is correct in that you can override the customMiddleware property of the express config. This is undocumented as we would prefer you to use policies but it's there if you absolutely need it.
Note config/local.js is in the .gitignore file so if you place it there it won't be checked into git by default.
There are quite a number of cases where policies are not enough. Just to give an example, if i need to handle dynamic routes (in example, i want my API to route to the same controller, no matter if path is /v1/bla or //v1//bla or //v1////controller) i have no other choice but modify the custom express middleware. I coudnt find an example of how to create a custom hook (which seems the way to go to do it). In any case, i think providing docs or at least a code snippet would be useful.
@juancarloscancela I think you can do that with the config/routes.js file, see https://github.com/balderdashy/sails-docs/blob/master/concepts/Routes/RouteTargetSyntax.md
@blah238 I could eventually do that, but defining regex for any case can be actually quite hard, or at least, not trivial. Probably just adding to documentation how to configure a custom express middleware for this cases seems harmless, and could provide users with an alternative without having to use complex regexes.
"express:" is deprecated (in sails 0.12.1). use "http:"
http: {
customMiddleware: function (app) {
console.log("config of Middleware is called")
app.use(function (req, res, next) {
console.log("installed customMiddleware is used")
next()
})
}
}
how would I serve static files this way?
@grimace You can serv static files this way:
var express = require('../node_modules/sails/node_modules/express');
module.exports.express = {
middleware: {
custom: true
},
customMiddleware: function (app) {
app.use(express.logger());
app.use(express.compress());
app.use('/api/docs',express.static('assets/swagger-ui/dist/'));
}
};
This is my config/express.js file
How to make it in 0.12? Any of listed isn`t working at all.
@Crusader4Christ - I believe you can plug additional middleware in config/http.js.
You define a middleware and then add it to the order array. Take a look here:
http://sailsjs.com/documentation/concepts/middleware
If you need lower level control, it is also possible (from what I've seen online) though it's out of my league :-)
Hi all, I'm trying to use custom middleware, I've this code
myCustomMiddleware: function (req, res, next) {
console.log("Log myCustomMiddleware");
return next();
},
and added 'myCustomMiddleware' to the 'order' array, but in development mode there's no output for the console log, and can't figure out if middleware is working or not. Tried also to use 'custom:true' inside middleware.
Any idea? Thank you
update: solved, sorry. I've placed myCustomMiddleware outsite middleware object
Hi, I made a question related with this on Stackoverflow. @Crusader4Christ have you figured this out?
The method described by @alvaro-paco isn't working. I think this was the way on older Sails.js versions. In my case, I need to specify the path for the middleware.
const express = require('@sailshq/express');
middleware: {
staticDocs: express.static('assets'),
order: [
'startRequestTimer',
// 'cookieParser',
// 'session',
'staticDocs',
Has anyone tried to add custom middleware in sails1.0? I need to hook my own middleware for which I need access to the express app handle. Specifically, I need something like:
var bodyParser = require('body-parser');
expressApp.use(bodyParser.json(), myMiddleware)
How to get access to expressApp object from Sails?
same problem with custom middleware (swagger)
@kavuri @solovieff If you take a look at the native hook, express gets mounted on sails.hooks.http.app, here is an example I used to add the web pack dev server Sails 1.0+ Express App.use, Webpack Dev Server Hook
@grimace You can serv static files this way:
var express = require('../node_modules/sails/node_modules/express'); module.exports.express = { middleware: { custom: true }, customMiddleware: function (app) { app.use(express.logger()); app.use(express.compress()); app.use('/api/docs',express.static('assets/swagger-ui/dist/')); } };This is my config/express.js file
I have done this but getting this error
"Detected a custom middleware custom that does not appear in the
middleware order. Please add custom to sails.config.http.middleware.order."
Most helpful comment
Has anyone tried to add custom middleware in sails1.0? I need to hook my own middleware for which I need access to the express app handle. Specifically, I need something like:
How to get access to
expressAppobject from Sails?