Skip to content

Instantly share code, notes, and snippets.

@zdying
Last active March 15, 2018 08:22
Show Gist options
  • Save zdying/9cfb806278c401e269427554e8fb15d1 to your computer and use it in GitHub Desktop.
Save zdying/9cfb806278c401e269427554e8fb15d1 to your computer and use it in GitHub Desktop.
modify response content
const fs = require('fs');
const path = require('path');
const http = require('http');
var server = http.createServer((req, res) => {
const oldEnd = res.end;
let body = [];
let isString = false;
res.write = function (chunk) {
if (typeof chunk === 'string') {
isString = true;
}
body.push(chunk);
};
res.end = function () {
body = isString ? body.join('') : Buffer.concat(body).toString();
oldEnd.call(res, '<!-- you have been hacked. -->' + body);
};
fs.createReadStream(path.join(__dirname, 'index.html')).pipe(res);
});
server.listen(8888);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment