Number of consultations held Nodejs

Asked

Viewed 372 times

0

I would like to know the best way to get the number of requests made to a nodejs server since the last Restart.

For example:

  • Admin: Node index.js
  • User: 127.0.0.1:3000/all
  • User2: 127.0.0.1:3000/all
  • User3: 127.0.0.1:3000/all

x = 3 (number of requests made since last Restart)

1 answer

1


Create a middleware to carry out the count:

const contador = 0;

const adicionar = async (req, res, next) => {
  contador += 1;
  next();
};

const contar = () => {
  return contador;
};

module.exports = {
  adicionar,
  contar,
};

Where you start your routes:

const { adicionar, contar } = require('./middleware');

// ...
app.use(adicionar);

And to know how many requests have been made use the function contar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.