swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

How to add path specific express middleware?

Open jsdevel opened this issue 11 years ago • 13 comments

I'm not seeing any documentation on how I can add middleware on a path by path basis.

jsdevel avatar Aug 18 '15 18:08 jsdevel

I'm working on a more integrated solution, but there are two options currently:

  1. Execute the middleware in your controller: middleware(req, res, next);
  2. Add the middleware in app.js: app.use();

Scott

theganyo avatar Aug 18 '15 19:08 theganyo

Option 1 feels dirty, as you'd have to use async Option 2 only applies to the global app correct?

jsdevel avatar Aug 18 '15 21:08 jsdevel

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...
  }
}

theganyo avatar Aug 18 '15 21:08 theganyo

I've had upwards of 5 pieces of middleware in the past, which would lead to calllback hell without async.

jsdevel avatar Aug 18 '15 22:08 jsdevel

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);

theganyo avatar Aug 18 '15 23:08 theganyo

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);
    }

jsdevel avatar Aug 18 '15 23:08 jsdevel

That's an interesting idea. I'll definitely give it some thought. Thanks!

theganyo avatar Aug 18 '15 23:08 theganyo

Looks like krakenjs/swaggerize-express follows the array approach: https://github.com/krakenjs/swaggerize-express#handler-middleware

jsdevel avatar Aug 19 '15 18:08 jsdevel

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.

theganyo avatar Sep 11 '15 22:09 theganyo

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.

jsdevel avatar Sep 11 '15 22:09 jsdevel

Not at all. All pipes, and therefore the ordering of the modules, are defined as an array.

theganyo avatar Sep 11 '15 23:09 theganyo

@theganyo please provide an example how to use these bagpipes for a specific path. Thank you!

olegmdev avatar Jan 20 '17 23:01 olegmdev

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]); }
        );
      }
    }
  }
}

erichulburd avatar Mar 07 '17 23:03 erichulburd