Skip to content

Instantly share code, notes, and snippets.

@geraintwhite
Created November 20, 2014 15:08
Show Gist options
  • Select an option

  • Save geraintwhite/40641eada16ab15e9b50 to your computer and use it in GitHub Desktop.

Select an option

Save geraintwhite/40641eada16ab15e9b50 to your computer and use it in GitHub Desktop.
NodeJS HTTP server testing
var http = require('http'),
bl = require('bl');
function handler(req, res) {
req.pipe(bl(function (err, data) {
if (err) {
console.log(err)
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end('Error receiving data');
return False
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.parse(data));
}));
}
if (require.main == module) {
http.createServer(handler).listen(8080);
}
module.exports = handler;
var handler = require('./'),
through2 = require('through2');
var req = through2(),
payload = {gh: 1, obj: 'data'};
req.url = 'localhost';
var res = {
writeHead: function(code, type) { console.log(code, type); },
end: function(data) { console.log(data); }
}
handler(req, res);
req.end(JSON.stringify(payload))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment