aichbauer / express-routes-mapper

a small mapper for express routes
MIT License
21 stars 16 forks source link

Execute middlewares before executing endpoint's method #20

Closed toksdotdev closed 5 years ago

toksdotdev commented 6 years ago

I feel it would be awesome if we could execute a middleware before entering a specific endpoint method in a controller. I know there are work around this, but this should be easily available.

e.g. something of this fashion:

app.post('/user', [ validate_some_stuffs(...) ], (req, res) => {  // validation middle-ware executes first

});
aichbauer commented 6 years ago

That would be a awesome feature 👍

Any idea on how we could structure these middleware? I am open for ideas 😀

toksdotdev commented 6 years ago

Having thought about this, I feel for backward compatibility, we could check:

const routes = {
  'POST /user': 'UserController.create', // this old style still supported

  'GET /user:id': {
    path: 'UserController.get',
    middlewares: [
      // An array of middle wares could be specified 
      // without the open & closing brackets i.e. ()

      // e.g. 
      // 
      // checkIfAuthenticated   ----> correct
      // checkIfAuthenticated() ----> wrong   

      // The later is wrong because the middle-ware would be triggered
      // on `const routes` initialization, instead of before the controller
      // method execution and would prevent the middleware from accessing the 
      // `request` and `response` params that could be passed.
      // 
    ]
  },
};

export default routes;

Middle-wares would be executed in descending order for simplicity.

Final example

const routes = {
  'GET /user:id': {
    path: 'UserController.get',
    middlewares: [
         checkIfAutheticated,
         verifyFacebookAuth,  // this would come in handy for passport authentication
    ],

  'POST /user': 'UserController.create'
  },
};

Please feel free to correct me were I might be wrong...

toksdotdev commented 6 years ago

This middleware execution could be executed right before this code clock below is called, with some slight modifications.

https://github.com/aichbauer/express-routes-mapper/blob/a190d079ad3342bd5658e5e2fc8efdad07166d30/src/index.js#L49-L57

I could send in a PR if you deem this feature fit...

aichbauer commented 6 years ago

Hey @TNkemdilim,

this sounds great. Send a PR and we can see if there is something we can improve...

toksdotdev commented 6 years ago

Gotcha!!!

toksdotdev commented 6 years ago

Sorry, should send in a PR shortly, as I've been quite busy.

aichbauer commented 6 years ago

@TNkemdilim is there any update on this issue? I really like the idea of executing middleware before a specific route.

joneldiablo commented 6 years ago

Hi! I have this code:

app.use('/public', mapRoutes(config.publicRoutes, './controllers/'));
app.use('/private', config.verifyToken, mapRoutes(config.privateRoutes, './controllers/'));

but I can't get access to req.params in config.verifyToken function, the params not exist before mapRoutes, any idea how I can evaluate path params like /users/:ID?

toksdotdev commented 6 years ago

I'm currently working on a PR for this; I'll see if I can wrap up, and submit for review immediately.

Sorry for the delay @aichbauer & @joneldiablo

toksdotdev commented 6 years ago

I just added the feature; it's awaiting review. @aichbauer and @joneldiablo

aichbauer commented 6 years ago

@TNkemdilim I will review it, during this week or next week. Thank you for your contribution 👍

joneldiablo commented 6 years ago

hi @TNkemdilim @aichbauer yesterday I did a pull request to the TNkemdilim fork, to use a middleware for all routes

alkanyunus commented 5 years ago

@joneldiablo so we can't use it yet :/ waiting...

joneldiablo commented 5 years ago

@saxahan you can use the @TNkemdilim fork XD or wait for @aichbauer accept the pr XD

toksdotdev commented 5 years ago

Just made necessary updates to the PR...

aichbauer commented 5 years ago

I'll review it tomorrow. if everything is okay i'll release a new version tomorrow.