User Tools

Site Tools


njsetn:making_a_request

7.01. Making a request

→ nodejs.org/api/http.html

→ nodejs.org/api/https.html

HTTP/HTTPS : un module par protocole, en https on doit fournir un certificat.

var https = require("https");
var fs = require("fs");

var options = {
  hostname: "en.wikipedia.org",
  port: "443", // le port https est souvent 443, peut varier selon les sites
  path: "/wiki/George_Washington",
  method: "GET",
};

var res = https.request(options, function(res) {

  var responseBody = "";

  console.log('Response from server started.');
  console.log(`Server status:${res.statusCode}`);
  console.log(`Response headers: %j`, res.headers);

  res.setEncoding('UTF-8');

  res.once('data', function(chunk) {
    console.log(chunk);
  });

  res.on('data', function(chunk) {
    console.log(`--chunk--${chubk.length}`)
    responseBody += chunk;
  });

  res.on("end", function() {
    fs.wrtieFile("george-washington.html", responseBody, function(error) {
      if (error) {
        throw err;
      }
      console.log("file Downloaded");
    }
  });

});

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

req.end();
njsetn/making_a_request.txt · Last modified: 2016/11/23 18:36 by leo