39
loading...
This website collects cookies to deliver better user experience
SSL_CERT_FILE
and SSL_KEY_FILE
to the generated files.mkcert -install
.mkcert localhost
.key
and cert
properties. Hence, after generating the local certificate authority and ssl certificate we have to set the key
and cert
properties to the path of the certificate and key files.CERT-PATH
and KEY-PATH
are the paths to the generated files.mkdir node-ssl-test
npm init -y
to be able to install node packages.cd node-ssl-test
npm init -y
express
.npm install express
index.js
file.touch index.js
const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
const options = {
key: fs.readFileSync(CERT_PATH),
cert: fs.readFileSync(KEY_PATH),
};
app.use((req, res, next) => {
res.send('<h1>HTTPS is working!</h1>');
});
const port = 3000;
https.createServer(options, app).listen(port, () => {
console.log('Server listening on port ' + port);
});
index.js
file with node index.js
and open a browser tab and navigate to https://localhost:3000
, you should see HTTPS works!. You can also inspect the certificate in the browser dev tools (Chrome -> Security Tab or Lock Icon).