Aria Ak
Aria Ak

Reputation: 65

Getting an error on making Https request using NodeJS

I am trying out a NodeJS sample file from a tutorial as I am new to NodeJS but I just can't make it work. I get an error when I run below code:

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

var options = {
    hostname: "en.wikipedia.org",
    port: 443,
    path: "/wiki/George_Washington",
    method: "GET",
}

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

req.on('error', function(e) {
    console.log(`here comes the error ${e}`);
});

I get the following error:

 here comes the error:
 Error:101057795:error:140770FC:SSLroutines:SSL23_GET_SERVunknownprotocol:openssl\ssl\s23_clnt.c:794:

I am clueless, I appreciate your help and insight :)

Update: Code had to be modified to go through a proxy server. I posted the solution and findings.

Upvotes: 0

Views: 1400

Answers (2)

Aria Ak
Aria Ak

Reputation: 65

I solved the issue. Here is a summary of the problem and the solution that worked for me.

I ran this code in another network and it worked fine. So I realized the issue is that I am running the code behind our corporate web proxy and I need to modify my code to go through the proxy system instead of making a direct connection to target web server. I tried to install https-proxy-agent module but it failed to install. Again, because I am behind the proxy system. There is a npm config settings file named .npmrc file which can be found under C:/users/[YOUR_USER]. I added below configs to .npmrc to be able to install new packages as was advised Here.

proxy = http://172.26.128.35:3128/
https_proxy = http://172.26.128.35:3128/
strict-ssl = false
ca = null
registry = http://registry.npmjs.org/

Finally I modified my code as below to make it go through and the proxy system and voila~, It worked like a charm. If you encountered this issue, I hope this helps.

var https = require("https");
var fs =    require("fs");
var HttpsProxyAgent = require('https-proxy-agent');
var proxy = 'http://172.26.128.35:3128';
var agent = new HttpsProxyAgent(proxy);

var options = {
    hostname: "en.wikipedia.org",
    port: 443,
    path: "/wiki/George_Washington",
    method: "GET",
    agent: agent
}

var req = https.request(options, function(res) {
  console.log(res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
    console.log(`here comes the error ${e}`);
});

Upvotes: 1

Celso Agra
Celso Agra

Reputation: 1499

Have you tried this code?

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

var options = {
    hostname: "en.wikipedia.org",
    port: 443,
    path: "/wiki/George_Washington",
    method: "GET",
}

var req = https.request(options, function(res) {
  console.log(res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
    console.log(`here comes the error ${e}`);
});

Works fine for me!

Upvotes: 0

Related Questions