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

express-middleware #17

Closed
Couto opened this issue Jul 3, 2017 · 1 comment
Closed

express-middleware #17

Couto opened this issue Jul 3, 2017 · 1 comment

Comments

@Couto
Copy link

Couto commented Jul 3, 2017

One use case that seems to become a pattern in expressjs applications is to measure the interval between receiving and responding to a request.

This pattern can be abstracted with a simple middleware function similar to:

function timer(start = new Date()) {
  return (end = new Date()) => (end - start);
}

module.exports = (client) => (req, res, next) => {
  req.__timer = timer();
  res.on('finish', () => client.timer('response_time', req.__timer()));
  next();
};

And could be used in a similar manner to:

const Statful = require('statful');
const statfulMiddleware = require('statful/middleware')
const statfulClient = new Statful(/*config*/);
const app = express();

app.use(statfulMiddlware(statfulClient))

I can start a PR with this, but I was wondering if it's desirable to add this directly to the statful client, or if it should be in a separate project.

@miguelcnf
Copy link
Contributor

Yes this makes a lot of sense. I'd prefer we do it as a standalone repo with a dependency on the client.

I've just invited you to the team, feel free to create a new statful-middleware-express repo.

I appreciate this.

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

2 participants