http-intercept
Enhancement for Node's HTTP IncomingMessage & ServerResponse to let the data going from/to those streams to be intercepted - and modified... :)
Last updated 13 years ago by pvoisin .
MIT · Repository · Original npm · Tarball · package.json
$ npm install http-intercept 
SYNC missed versions from official npm registry.

Usage

Approach is pretty simple: just call the intercept method on either request or response and register for the "interception" event (or just pass your callback when calling intercept). When it is triggered you'll get the intercepted data. At that point you could do whatever you want with it, then normal flow of things will resume...

Modifying Request Data

Let's say you want to change incoming data:

var HTTP = require("http");
var Query = require("querystring");
var read = require("concat-stream");

var server = HTTP.createServer(function(request, response) {
//*
	request.intercept(function(context) {
		context.buffer = "user=Jack";
	});
//*/

	request.pipe(read(function(data) {
		var query = Query.parse(String(data) || "");
		response.end("Hello, " + query.user + "!");
	}));
}).listen(80);

Note: post something like "user=Barnabé" and you'll see that it is turned into "Jack" in the greeting message.

Modifying Response Data

var HTTP = require("http");
var Query = require("querystring");
var read = require("concat-stream");

var server = HTTP.createServer(function(request, response) {
	var user;
//*
	request.intercept(function(context) {
		user = Query.parse(String(context.buffer) || "").user;
		context.buffer = "user=Jack";
	});
//*/

//*
	response.intercept(function(context) {
		context.buffer = "Well... I guess you're not " + query.user + ", but " + user + ", right?";
	});
//*/

	var query;

	request.pipe(read(function(data) {
		query = Query.parse(String(data) || "");
		response.end("Hello, " + query.user + "!");
	}));
}).listen(80);

Current Tags

  • 0.1.1                                ...           latest (12 years ago)

2 Versions

  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           13 years ago
Maintainers (1)
Downloads
Total 3
Today 2
This Week 3
This Month 3
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE