http client

suggest change

A basic HTTP client. It expects the HTTP server example to be listening on port 80.

Write the following code in http_client.js:

var http = require('http');

var options = {
  hostname: '127.0.0.1',
  port: 80,
  path: '/',
  method: 'GET'
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
    });
    res.on('end', function () {
        console.log('Response ENDED');
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});


req.end();

then from your http_client.js location run this command:

node http_client.js

you should see this result:

STATUS: 200
HEADERS: {"content-type":"text/plain","date":"Thu, 21 Jul 2016 11:27:17 GMT","connection":"close","transfer-encoding":"chunked"}
Response: OK
Response ENDED

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents