Skip to content

Instantly share code, notes, and snippets.

@theothermattm
Created January 30, 2014 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theothermattm/8707590 to your computer and use it in GitHub Desktop.
Save theothermattm/8707590 to your computer and use it in GitHub Desktop.
Simple node.js app that transforms a pages content using streams. Uses npm request and npm express. http://localhost:3000/replace?url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions&match=Regular
var express = require('express');
var app = express();
app.get('/replace', function(req, res) {
var request = require('request');
var stream = require('stream');
var zlib = require('zlib');
var url = req.query.url;
var match = req.query.match;
console.log("Calling url " + url + " and matching " + match);
var Transform = require('stream').Transform;
var replacer = new Transform();
replacer._transform = function(chunk, encoding, done) {
var data = chunk.toString();
data = data.replace(new RegExp(match, 'g'),'MATCHED');
console.log("replacing some stuff");
this.push(data);
done();
}
res.setHeader('Content-Type', 'text/html');
req.pipe(request(url)).pipe(zlib.createGunzip()).pipe(replacer).pipe(res);
}).listen(3000);
console.log("Listening on 3000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment