Skip to content

Instantly share code, notes, and snippets.

@nyg
Created December 29, 2023 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyg/b1fd4709d8c62d2a56d850cd6c8c0a53 to your computer and use it in GitHub Desktop.
Save nyg/b1fd4709d8c62d2a56d850cd6c8c0a53 to your computer and use it in GitHub Desktop.
HTTP request with Node.js
import http from 'http'
const options = {
hostname: 'perdu.com',
method: 'GET',
}
const req = http.request(options, res => {
console.log(`Status: ${res.statusCode}`)
console.log(`Headers: ${JSON.stringify(res.headers, null, ' ')}\n`)
res.on('data', chunk => {
console.log(`Body:\n${chunk}`)
})
res.on('end', () => {
console.log('No more data in response.')
})
})
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`)
})
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment