-
Notifications
You must be signed in to change notification settings - Fork 44
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
Pass middleware per route #127
Comments
One thing to note is that if routes pass over a microservice boundary, any functions will be stripped (functions can't be transported via json). That said, you could proxy I'd like to see how this would be implemented in the various adapters though. For the connect-style frameworks it's easy enough to prepend middleware, but I don't have a lot of experience with HAPI and I'm not sure off hand how this would look... Thoughts? |
Can you elaborate how that can be achieved by proxying |
An example of using prior to proxy calls might look something like this: seneca = Seneca()
seneca.use(SenecaWeb, {...etc...})
seneca.ready(() => {
seneca.add('role:web,routes:*', function (msg, cb) {
msg.routes[0].map.authRoute.middleware = [
(req, res, next) => {...etc...}
]
this.prior(msg, cb)
})
}) Assuming the message comes in looking something like this: {
"routes": [
{
"pin": "cmd:*",
"map": {
"authRoute": {"GET": true}
}
}
]
} It would modify the Another option might be to add a more specific pin like seneca.add('role:web,auth:true,route:*', (msg, cb) => {
const routes = msg.routes
// todo: modify all incoming routes to include middleware
seneca.act('role:web', {routes})
}) Finally, if you don't care about ALL routes having the same middleware, you can attach it to whatever was provided via the seneca.use(SenecaWeb, {
...etc...
context: new Router()
})
seneca.ready(() => {
const app = express()
const router = seneca.export('web/context')()
router.use((req, res, next) => {...etc...})
app.use('/api', router)
}) |
I like the first approach, I think to anticipate maintenance problems, lets pass collection of middleware instead of specific middleware. Something like this:
Incoming Message
Then in the
Then adapter implementators will inspect route to find if there is middleware key then extract middleware from middleware array and apply it in the context. That way will enable middleware to be applied per specific route and not to all routes. |
Terribly sorry for not responding earlier. My main concern with this is it changes the API contract between Also, I liked the other way a bit more - providing functions.... but, at the same time, I can see value in providing named keys as well. How about both? We could check if the entry is a string - if so, pull in the middleware -- if it's a function, assume that it is middleware and use that. Of course that concern is more in the adapters and how they deal with this new information. In terms of
Also, we're going to need docs that describe this pretty well I think, another page under ./docs/ called I'll respond also in the PR with specific things that need to happen. |
I appreciate the great work that has been done by the team behind seneca well i have little suggestion if possible to put a way for implementers to be to pass middleware per route. Forexample I prefer my own auth middleware over passport but I don't see a way I can pass my middleware per route
The text was updated successfully, but these errors were encountered: