Last active
March 15, 2018 08:22
-
-
Save zdying/9cfb806278c401e269427554e8fb15d1 to your computer and use it in GitHub Desktop.
modify response content
This file contains hidden or 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
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