OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

SSL Error : net::ERR_CERT_AUTHORITY_INVALID while loading Express and NodeJS API

  • Thread starter Thread starter bocan
  • Start date Start date
B

bocan

Guest
I've been facing this error and even after a lot of research on SO, I have not been able to solve it. Here what I did :

  • I first have followed the steps indicated here in order to create my self-signed certificate.
  • Then, I added it to my Node.js server code :

Code:
const express = require('express');
const fs = require('fs');
const cors = require('cors');
const csvParser = require('csv-parser');
const https = require('https');

const app = express();
const port = 30440;

app.get('/read-data', (req, res) => {
    const results = [];

    fs.createReadStream('data.csv')
        .pipe(csvParser())
        .on('data', (data) => results.push(data))
        .on('end', () => {
            res.json(results);
        })
        .on('error', (error) => {
            console.error('Error reading CSV:', error);
            res.status(500).send('Error reading CSV');
        });
});

const private = fs.readFileSync('client-key.pem', 'utf8');
const cert = fs.readFileSync('client-cert.pem', 'utf8');

const certificate = {key: private, cert: cert};

https.createServer(certificate, app).listen(port, '0.0.0.0', () => {
        console.log(`Server is running on https://grumpycoincat.xyz:${port}`);
})
  • And here is a part of the client code :

Code:
const response = fetch('https://109.123.248.250:30440/read-data')

I'm currently testing in production, because in development the browser doesn't care about the localhost.

Thanks in advance

<p>I've been facing this error and even after a lot of research on SO, I have not been able to solve it. Here what I did :</p>
<ul>
<li>I first have followed the steps indicated <a href="https://www.golinuxcloud.com/create-https-server-with-node-js/#Step3_Generate_an_SSL_certificate" rel="nofollow noreferrer">here</a> in order to create my self-signed certificate.</li>
<li>Then, I added it to my Node.js server code :</li>
</ul>
<pre><code>const express = require('express');
const fs = require('fs');
const cors = require('cors');
const csvParser = require('csv-parser');
const https = require('https');

const app = express();
const port = 30440;

app.get('/read-data', (req, res) => {
const results = [];

fs.createReadStream('data.csv')
.pipe(csvParser())
.on('data', (data) => results.push(data))
.on('end', () => {
res.json(results);
})
.on('error', (error) => {
console.error('Error reading CSV:', error);
res.status(500).send('Error reading CSV');
});
});

const private = fs.readFileSync('client-key.pem', 'utf8');
const cert = fs.readFileSync('client-cert.pem', 'utf8');

const certificate = {key: private, cert: cert};

https.createServer(certificate, app).listen(port, '0.0.0.0', () => {
console.log(`Server is running on https://grumpycoincat.xyz:${port}`);
})
</code></pre>
<ul>
<li>And here is a part of the client code :</li>
</ul>
<pre><code>const response = fetch('https://109.123.248.250:30440/read-data')
</code></pre>
<p>I'm currently testing in production, because in development the browser doesn't care about the localhost.</p>
<p>Thanks in advance</p>
 

Latest posts

Top