-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Middleware/ext support #34
Comments
I believe I understand. In your Can you show a usage example with your proposed API to clarify how it would work from the user's perspective? It's like the code below and it would be instantiated for each request, right? class Middleware {
constructor(h) {
this.foo = 'could store some config here'
}
onPreHandler(request) {
this.time = 'could set up a timer here to measure how long the handler takes to execute'
}
onPostHandler(request) {
this.time = 'could stop a timer here and measure how long the handler took to execute'
}
// ...
}
pogo.server({
ext(Middleware)
}); The above is a little too object-oriented for my liking. But I think I see the appeal. The way that hapi lets you keep track of state like timings is with |
That example was pretty much what I was thinking, but whether it's worth implementing is mostly down to how well it fits. Using an API like Hapi's For |
Middleware support would be really great, It'd be really nice if I could log requests I've gotten to either a file or a db |
@himbolion agreed, that's one of the top use cases here. The first step is to implement an event system so you can do this: server.on('request', (request) => {
log(request);
}); Then the second step is to implement const myPlugin = (server) => {
server.on('request', (request) => {
log(request);
});
};
server.register(myPlugin); Plugins are just functions that are |
It would be good to support some form of middleware to allow processing of requests/responses in a more general fashion. Hapi uses the following (with some extra overloads):
A suggestion I had was to use a modified version, which uses class instances for each request. That way you can keep track of information during a request (such as timing), and allows for an API like so:
It should be possible to support both, but that would complicate things possibly. Another option would be to support
in some way to allow for the case where you don't need to store instance data (if this was supported, each method would also get
h
as well).The text was updated successfully, but these errors were encountered: