Closed toksdotdev closed 5 years ago
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:
If the value of 'GET /user:id'
which is ('UserController.get'
) is of type String
, take the value as the controller and method
i.e. 'GET /user:id': 'UserController.get'
Else use the new method we want to adopt below:
Note each middleware would receive both the
request
,response
object and optionally anext
callback
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.
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:
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?
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 XD
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: