-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Execute middlewares before executing endpoint's method #20
Comments
That would be a awesome feature 👍 Any idea on how we could structure these middleware? I am open for ideas 😀 |
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... |
This middleware execution could be executed right before this code clock below is called, with some slight modifications. express-routes-mapper/src/index.js Lines 49 to 57 in a190d07
I could send in a PR if you deem this feature fit... |
Hey @tnkemdilim, this sounds great. Send a PR and we can see if there is something we can improve... |
Gotcha!!! |
Sorry, should send in a PR shortly, as I've been quite busy. |
@tnkemdilim is there any update on this issue? I really like the idea of executing middleware before a specific route. |
Hi! I have this code:
but I can't get access to req.params in |
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 |
I just added the feature; it's awaiting review. @aichbauer and @joneldiablo |
@tnkemdilim I will review it, during this week or next week. Thank you for your contribution 👍 |
hi @tnkemdilim @aichbauer yesterday I did a pull request to the TNkemdilim fork, to use a middleware for all routes |
@joneldiablo so we can't use it yet :/ waiting... |
@saxahan you can use the @tnkemdilim fork XD or wait for @aichbauer accept the pr |
Just made necessary updates to the PR... |
I'll review it tomorrow. if everything is okay i'll release a new version tomorrow. |
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:
The text was updated successfully, but these errors were encountered: