Gathering detailed insights and metrics for express-interceptor
Gathering detailed insights and metrics for express-interceptor
Gathering detailed insights and metrics for express-interceptor
Gathering detailed insights and metrics for express-interceptor
@metis-data/sequelize-express-interceptor
Intercept sequelize queries and express requests using OpenTelemetry, enrich spans and send them to metis platform
@metis-data/prisma-express-interceptor
Metis package for prisma and express interception
express-intercept
Build Express middleware to intercept / replace / inspect / transform response
@enjoys/express-interceptor
express interceptor to change/update body response
npm install express-interceptor
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
79 Stars
92 Commits
22 Forks
66 Watchers
1 Branches
18 Contributors
Updated on May 16, 2024
Latest Version
1.2.0
Package Id
express-interceptor@1.2.0
Size
7.79 kB
NPM Version
2.14.20
Node Version
4.4.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
6
A tiny Express response interceptor
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:
Install the package
npm i --save express-interceptor
Define your interceptor
1var express = require('express'); 2var cheerio = require('cheerio'); 3var interceptor = require('express-interceptor'); 4 5var app = express(); 6 7var finalParagraphInterceptor = interceptor(function(req, res){ 8 return { 9 // Only HTML responses will be intercepted 10 isInterceptable: function(){ 11 return /text\/html/.test(res.get('Content-Type')); 12 }, 13 // Appends a paragraph at the end of the response body 14 intercept: function(body, send) { 15 var $document = cheerio.load(body); 16 $document('body').append('<p>From interceptor!</p>'); 17 18 send($document.html()); 19 } 20 }; 21}) 22 23// Add the interceptor middleware 24app.use(finalParagraphInterceptor); 25 26app.use(express.static(__dirname + '/public/')); 27 28app.listen(3000); 29
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:
1<html> 2<head></head> 3<body> 4 <p>"Hello world"</p> 5</body> 6</html>
Into:
1<html> 2<head></head> 3<body> 4 <p>"Hello world"</p> 5 <p>From interceptor!</p> 6</body> 7</html>
See more examples. Also, you can debug express-interceptor actions using debug env variable DEBUG=express-interceptor
.
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.
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.
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.
Please check CONTRIBUTING.md.
This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More