4
var options = {
  key: fs.readFileSync("./cert/server.key"),
  cert: fs.readFileSync("./cert/server.cert")
};

app.get("/", (req, res) => {
  console.log("test 1");
  res.send("Hello World111!");
});


var server = https.createServer(options, app);

server.listen(port, function() {
  console.log("Express server listening on port " + server.address().port);
});

The above is the code which uses a self signed certificate.

When I try to access the API from a different NodeJS application as follow.

try {
    const agent = new https.Agent({
      requestCert: true,
      rejectUnauthorized: false,
       ca: fs.readFileSync("./cert/server.pem")
    });
    let response = await axios.get(url, { httpsAgent: agent });
    if (response.status == 200) {
      return await response.data;
    }
  } catch (error) {
    console.log(error, "Error in getAuth Token");
    throw new Error("Network Error, Error receiving token");
  }

I am getting the following error:

 Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1055:34)
    at TLSSocket.emit (events.js:198:13)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)
  code: 'DEPTH_ZERO_SELF_SIGNED_CERT',
  config:
   { url: 'https://localhost/',
     method: 'get',
     headers: 

I don't want to set rejectUnauthorized to true. or set any nodejs paramater to reject the certificate.

I would want the certificate to be accepted without any exception. Thanks in advance.

Mukund
  • 916
  • 2
  • 11
  • 18
  • Does this answer your question? [How to configure axios to use SSL certificate?](https://stackoverflow.com/questions/51363855/how-to-configure-axios-to-use-ssl-certificate) – jsN00b Jan 04 '22 at 12:38

1 Answers1

-1

i was with the same error i just change the ca to cert and worked for me

const agent = new https.Agent({
      requestCert: true,
      rejectUnauthorized: false,
       cert: fs.readFileSync("./cert/server.pem")
    });
  • 1
    Setting `rejectUnauthorized` to `false` makes the axios client ignore invalid certs. Not recommended from a security standpoint. – PatrikJ Oct 04 '22 at 13:45