How to add path specific express middleware?
I'm not seeing any documentation on how I can add middleware on a path by path basis.
I'm working on a more integrated solution, but there are two options currently:
- Execute the middleware in your controller:
middleware(req, res, next); - Add the middleware in app.js:
app.use();
Scott
Option 1 feels dirty, as you'd have to use async Option 2 only applies to the global app correct?
You don't have to use async. You can just incorporate it like this:
var middleware = require('otherCoolMiddleware');
function myController(req, res, next) {
middleware(req, res, function(err) {
if (err) { return next(err); }
// do whatever I need to do here...
}
}
I've had upwards of 5 pieces of middleware in the past, which would lead to calllback hell without async.
Hey, I'm just throwing out ideas... :)
And, yes, you can do it in app.js non-globally... but I don't believe you can use that format with use, I think you have to do it per-operation...
app.get('/path', middleware);
app.post('/path', middleware);
And I appreciate it!
Ok, adding the middleware like you've just described would likely work. I was thinking it would be nice to define it in the .yml, but that would proly be too express centric. I'll just have to add the middleware before swagger inits.
It would be awesome IMO if you supported exposing arrays for middleware in controller files. I.E.
var util = require('util');
module.exports = {
hello: [middleware1, middleware2, hello]
};
function hello(req, res) {
var name = req.swagger.params.name.value;
var hello = name ? util.format('Hello, %s', name) : 'Hello, stranger!';
res.json(hello);
}
That's an interesting idea. I'll definitely give it some thought. Thanks!
Looks like krakenjs/swaggerize-express follows the array approach: https://github.com/krakenjs/swaggerize-express#handler-middleware
Can you take a look at the new beta and let me know what you think? https://github.com/swagger-api/swagger-node/issues/294. This will allow you to run middleware in your pipeline you want. If you still think we need another way of doing what you'd like, let's discuss.
Looks like object literals, is that correct? If so, then I think it's relying on a behavior not guaranteed by the es standard (I.E. object literal properties should be returned in declaration order).
I've begun using this library with success and prefer it's approach a lot more: krakenjs/swaggerize-express.
Not at all. All pipes, and therefore the ordering of the modules, are defined as an array.
@theganyo please provide an example how to use these bagpipes for a specific path. Thank you!
I'm a little late here, but I ended up doing something along these lines:
var util = require('util');
module.exports = {
hello: stackMiddleware([middleware1, middleware2, hello])
};
function hello(req, res) {
var name = req.swagger.params.name.value;
var hello = name ? util.format('Hello, %s', name) : 'Hello, stranger!';
res.json(hello);
}
function stackMiddleware(middlewares){
return (req, res, next)=>{
const stack = [];
for (let i=middlewares.length; i--; i>0){
const middleware = middlewares[i];
if (i === 0){
middleware(req, res, stack[stack.length - i - 1]);
} else if (i === middlewares.length-1) {
stack.push(
()=>{ middleware(req, res, next); }
);
} else {
stack.push(
()=>{ middleware(req, res, stack[stack.length - i-1]); }
);
}
}
}
}