Last active
August 1, 2018 16:17
-
-
Save max-mapper/7167b27dde5588a83c47 to your computer and use it in GitHub Desktop.
http with require('net')
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 net = require('net') | |
var concat = require('concat-stream') | |
var pump = require('pump') | |
var reqBody = new Buffer(`GET / HTTP/1.1 | |
User-Agent: curl/7.37.1 | |
Host: localhost:8080 | |
Accept: */* | |
`) | |
http('localhost', 8080, reqBody, function (err, resp) { | |
if (err) throw err | |
console.log(resp.toString()) | |
}) | |
function http (host, port, body, cb) { | |
var socket = net.connect(port, host, function connected () { | |
// results in http response data being printed out correctly | |
socket.write(body) | |
socket.end() | |
// http response data never gets printed | |
// socket.end(body) | |
}) | |
var concatter = concat(function (resp) { | |
cb(null, resp) | |
}) | |
pump(socket, concatter, function (err) { | |
if (err) cb(err) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment