Created
November 20, 2014 15:08
-
-
Save geraintwhite/40641eada16ab15e9b50 to your computer and use it in GitHub Desktop.
NodeJS HTTP server testing
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
| 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; |
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
| 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