Created
January 30, 2014 12:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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