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.