HTTP Node Flashcards
1
Q
Can send a request using Node’s HTTP library.
A
https: //nodejs.org/api/http.html#http_http_get_options_callback
http. get( [ options ], [ function to handle response ] );
http.get( 'http://nodejs.org/dist/index.json', (res) => { const { statusCode } = res; const contentType = res.headers['content-type']; } );
http.get(options, (res) => { // Do stuff }).on('socket', (socket) => { socket.emit('agentRemove'); });
http.get({
hostname: ‘localhost’,
port: 80,
path: ‘/’,
agent: false // create a new agent just for this one request
}, (res) => {
// Do stuff with response
});
var http = require('http'); function getTestPersonaLoginCredentials(callback) {
return http.get({ host: 'personatestuser.org', path: '/email' }, function(response) { // Continuously update stream with data var body = ''; response.on('data', function(d) { body += d; }); response.on('end', function() {
// Data reception is done, do whatever with it! var parsed = JSON.parse(body); callback({ email: parsed.email, password: parsed.pass }); }); });
2
Q
How does one make HTTP requests to external API’s and resources.
A
pass the url of the server resource into the http.request( ).