Skip to content

Instantly share code, notes, and snippets.

@jcpst
Last active June 29, 2020 04:05
Show Gist options
  • Save jcpst/19a7ce5e7d23097baae533f592775a4c to your computer and use it in GitHub Desktop.
Save jcpst/19a7ce5e7d23097baae533f592775a4c to your computer and use it in GitHub Desktop.
Wrapper for node's built in http
'use strict'
const http = require('https')
function request(options, body) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let data = ''
res.setEncoding('utf8')
res.on('data', (chunk) => (data += chunk))
res.on('end', () => resolve(JSON.parse(data)))
})
req.on('error', (e) => reject(e.message))
if (body) req.write(body)
req.end()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment