The library should work starting Node.js 8.x, but testing is only expected for currently supported LTS versions of Node.js starting Node.js 12.x . use on your own risk with non LTS Node.js versions.
Typescript
type definitions are now included in this project, no extra steps required.
Example
This example will modify any search results coming from google and replace all the result titles with "Pwned!".
const Proxy = require('http-mitm-proxy').Proxy;// or using import/module (package.json -> "type": "module")// import { Proxy } from "http-mitm-proxy";const proxy = new Proxy();proxy.onError(function(ctx, err) { console.error('proxy error:', err);});proxy.onRequest(function(ctx, callback) { if (ctx.clientToProxyRequest.headers.host == 'www.google.com' && ctx.clientToProxyRequest.url.indexOf('/search') == 0) { ctx.use(Proxy.gunzip); ctx.onResponseData(function(ctx, chunk, callback) { chunk = Buffer.from(chunk.toString().replace(/<h3.*?<\/h3>/g, '<h3>Pwned!</h3>')); return callback(null, chunk); }); } return callback();});console.log('begin listening on 8081')proxy.listen({port: 8081});
Using node-forge allows the automatic generation of SSL certificates within the proxy. After running your app you will find options.sslCaDir + '/certs/ca.pem' which can be imported to your browser, phone, etc.