Skip to content
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

Run some code on every request #29

Closed
tim-phillips opened this issue Oct 19, 2017 · 2 comments
Closed

Run some code on every request #29

tim-phillips opened this issue Oct 19, 2017 · 2 comments

Comments

@tim-phillips
Copy link

What if I wanted to run some code on every request/route? For example, logging the headers to the console.

Is there any way to do this without having to put the code in every handler?

Thanks for the great library!

@tim-phillips
Copy link
Author

Recommended at the bottom of this comment is to use decorators as middleware. I suppose I could wrap router with log like so:

const { router, get } = require('microrouter')
const { log } = require('./my-middleware')

module.exports = log(router(
  get('/hello/:who', hello),
  get('/*', notfound)
))

I'll give this a shot.

@tim-phillips
Copy link
Author

The following higher order function worked with the above example:

const log = fn => (req, res) => {
  console.log(req.headers)
  fn(req, res)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant