I'm working on the Express API Javascript code in a way that uses the self-signed SSL certificate.
I created a self-signed SSL certificate using: https://helpcenter.gsx.com/hc/en-us/articles/115015960428-How-to-Generate-a-Self-Signed-Certificate-and-Private-Key-using-OpenSSL
Then, I put the privateKey.key
, certificate.crt
, and server.js
in the same folder.
Server.js
const express = require("express");
const fs = require("fs");
const app = express();
const port = 443;
app.get("/", (req, res) => {
res.send("IT'S WORKING!");
});
require("http")
.createServer(
{
key: fs.readFileSync("privateKey.key"),
cert: fs.readFileSync("certificate.crt"),
},
app
)
.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
I ran nodemon server.js in terminal and below is the output:
[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Listening on port 443!
I tested it in the browser by localhost:443
. Then the response IT'S WORKING
showed up.
My question is Am I doing correctly to the Express API Javascript code with the self-signed SSL certificate? How to know exactly the self-signed SSL certificate is being used?
Thank you so much