Skip to content

Instantly share code, notes, and snippets.

@warpech warpech/nodeapp.js
Created Feb 20, 2015

Embed
What would you like to do?
Man in the middle
var express = require('express')
var bodyParser = require('body-parser');
var chalk = require('chalk');
var app = express()
app.use(bodyParser.text());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, spy-type");
next();
});
app.post('/', function(req, res, next) {
// Handle the post for this route
if(typeof req.body == "object") {
req.body = JSON.stringify(req.body)
}
if(req.headers['spy-type'] == "response") {
console.log(chalk.cyan(req.body));
}
else {
console.log(chalk.green(req.body));
}
res.send();
});
app.listen(3000);
{
"name": "spyserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.12.0",
"chalk": "^0.5.1",
"express": "^4.11.2"
}
}
var spyServer = "http://localhost:3000/";
var _xhrsend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function () {
spyRequest.apply(this, arguments);
_xhrsend.apply(this, arguments);
}
var _wssend = WebSocket.prototype.send;
WebSocket.prototype.send = function () {
spyRequest.apply(this, arguments);
_wssend.apply(this, arguments);
if(!this.spied) {
this.addEventListener('message', function(ev) {
spyResponse.call(this, ev.data);
})
this.spied = true;
}
}
function spyRequest() {
var spyXhr = new XMLHttpRequest();
spyXhr.open("POST", spyServer);
spyXhr.setRequestHeader("spy-type", "request");
_xhrsend.apply(spyXhr, arguments);
}
function spyResponse() {
var spyXhr = new XMLHttpRequest();
spyXhr.open("POST", spyServer);
spyXhr.setRequestHeader("spy-type", "response");
_xhrsend.apply(spyXhr, arguments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.