1

From within a middleware module, I'm trying to count the number of page requests made from the parent app. Think of it as a generic request monitor that knows nothing about the pages the parent serves. My initial stab at it simply listened for requests and incremented a counter with each response generated, only to find that each page request generated n number of additional responses from all of the included requests (favicon.ico, script files, etc). I expected that, but hoped there was a way to filter out the secondary files.

Is there a way to differentiate between them so that I can ignore the included files in my count?

So far, I've used the request and express-req-metrics middlewares to look at the response properties, but haven't yet seen any property that was helpful.

1 Answer 1

1

If you're using middleware to serve static assets, then one solution is to just reorder your middleware so that static asset requests never make it to your counter middleware (if they are handled by upstream middleware):

var counter = 0;
app.use(serveStatic('public'));
app.use(function(req, res, next) {
  counter++;
  next();
})
// ... routes defined down here ...

However, one downside is that, if you don't have a favicon.ico file for example, the serveStatic middleware will not handle the request and your counter middleware will count those requests.

Another solution is to write your counter middleware so that it inspects the request path first to ensure that the path doesn't end in '.ico', '.js', '.jpg', etc. Here I just use a basic regular expression:

var counter = 0;
app.use(function(req, res, next) {
  if (! /(.ico|.js|.css|.jpg|.png)$/i.test(req.path)) {
    counter++;
  }
  next();
})
// ... routes defined down here ...
Sign up to request clarification or add additional context in comments.

3 Comments

Your 2nd solution is better for me, as my code won't know what types of assets are being served (and I'll have no knowledge of the code in the main app). That said, there's still the problem where, even though it filters out the non-html files, any given page may still include. html files (header.html, footer.html, etc). If I knew in advance which .html's to filter out, all would be well, but I won't.
If I understand you correctly, you must be talking about how your templating system allows you to include other template files into the current template. However, that's all done server-side and shouldn't be creating additional requests to your application. In other words, something sounds very wrong if code like {{ include 'header.html' }} in one template is hitting your middleware somehow. Am I misunderstanding?
I'm in agreement, and am curious as to why i'm getting these results, but alas I'm off on another problem. Will report back once I can return. Thanks for your help thus far @danneu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.