Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active August 1, 2018 16:17
Show Gist options
  • Save max-mapper/7167b27dde5588a83c47 to your computer and use it in GitHub Desktop.
Save max-mapper/7167b27dde5588a83c47 to your computer and use it in GitHub Desktop.
http with require('net')
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