First, make sure you have SSL sertificate and public key files.
- You can use
openssl req -nodes -new -x509 -keyout server.key -out server.cert
to generate self-signed certificate.
Add PATH_TO_FULL_CERT, PATH_TO_PRIVATE_KEY and HTTPS_PORT values in .env file:
PATH_TO_FULL_CERT=./certs/server.cert
PATH_TO_PRIVATE_KEY=./certs/server.key
HTTPS_PORT=5555
Then feed an express() listener with routing to the app.
require("dotenv").config()
const express = require('express')
// Require the package
const httpsServer = require('node-https')
// create express listener
const api = express()
// add any routing here, e.g. api.get("/", (req, res) => res.send("Server OK!")))
httpServer( api ) // now HTTPS server is running
alternatively, receive https object within callback
httpsServer( api, server => {
// do something with server, e.g. server.close()
})
or call asynchronously and receive server object as result
(
async () => {
const server = await httpsServer(api)
// do something with server
// ...
// close
server.close()
}
)()
npm test
runs a small test with server on port 5555.npm run test-live
does the same, but doesn't close the server after checking it, so that user can check the server manually.