Created
December 29, 2023 19:42
-
-
Save nyg/b1fd4709d8c62d2a56d850cd6c8c0a53 to your computer and use it in GitHub Desktop.
HTTP request with Node.js
This file contains 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
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