npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-interceptor

v1.2.0

Published

A tiny interceptor for Express responses

Downloads

196,953

Readme

express-interceptor

A tiny Express response interceptor

NPM

Build Status Dependencies

Express-interceptor allows you to define a previous step before sending a response. This allows you to do anything you want with the response, such as processing, transforming, replacing, or logging it. Express-interceptor allows you to avoid calling next() over and over. Further more, you can avoid managing nested scopes. Using a declarative API, it’s simple to use and maintain.

Some use cases include:

  • Transpile custom elements into standard HTML elements.
  • Transform JSX or compile less files on the fly.
  • Store statistics about responses.
  • Set response headers based on tags in the response body.
  • Dynamically inject live-reload scripts to HTML.

Usage

Install the package

npm i --save express-interceptor

Define your interceptor

var express     = require('express');
var cheerio     = require('cheerio');
var interceptor = require('express-interceptor');

var app = express();

var finalParagraphInterceptor = interceptor(function(req, res){
  return {
    // Only HTML responses will be intercepted
    isInterceptable: function(){
      return /text\/html/.test(res.get('Content-Type'));
    },
    // Appends a paragraph at the end of the response body
    intercept: function(body, send) {
      var $document = cheerio.load(body);
      $document('body').append('<p>From interceptor!</p>');

      send($document.html());
    }
  };
})

// Add the interceptor middleware
app.use(finalParagraphInterceptor);

app.use(express.static(__dirname + '/public/'));

app.listen(3000);

You're defining an interceptor that will be executed whenever the response contains html as Content-Type. If this is true, it will append a child at the end of the body. In other words, it will transform the response:

<html>
<head></head>
<body>
  <p>"Hello world"</p>
</body>
</html>

Into:

<html>
<head></head>
<body>
  <p>"Hello world"</p>
  <p>From interceptor!</p>
</body>
</html>

See more examples. Also, you can debug express-interceptor actions using debug env variable DEBUG=express-interceptor.

API

  • isInterceptable() (required): is a predicate function where you define a condition whether or not to intercept a response. Returning true buffers the request, and proceeds calling intercept() as well as afterSend(). Typically, you want to check for this condition in the res object in the definition of the middleware.

  • intercept(body, send) (required): Parse the body as an encoded string. After processing the body, call send(newBody) with the content to be sent back to the client.

  • afterSend(oldBody, newBody): This method will be called after sending the response to the client – after the done() callback in the send() method is executed. This method would typically be used to cache something, log stats, fire a job, among other things.

Who is using it?

Similar to

  • express-hijackresponse Different API, using callbacks with no top down structure, more obtrusive to HTTP internals.

  • hijackresponse Attempting to solve same problems than express-hijackresponse. Different API using pipes, recommended if you have problems with streaming or if you need more control over responses.

  • tamper Similar functionality but different internals and API.

Words of advice

If your intercept method make calls to a database, or needs to make expensive transformations to the original response, you should take in account the time that will add to the response. You should define your isInterceptable method carefully, checking the type of the response, or even the route that was fired by the request, also you may consider implementing a cache strategy to get faster responses.

If you face any issue, don't hesitate to submit it here.

Contributing

Please check CONTRIBUTING.md.

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.

Author

Collaborators

License

MIT