Skip to content

Instantly share code, notes, and snippets.

@eduzera
Created September 1, 2021 16:18
Show Gist options
  • Save eduzera/8983fc75d48ab8eb74f9fb74a05b2b59 to your computer and use it in GitHub Desktop.
Save eduzera/8983fc75d48ab8eb74f9fb74a05b2b59 to your computer and use it in GitHub Desktop.
nodejs - SSL for localhost

SLL for nodejs app on localhost

Pre requisites

install

  • create security folder
mkdir security

cd ./security
  • create certificates and set the password
mkcert -install

mkcert -key-file key.pem -cert-file cert.pem localhost
const express = require('express')
const app = express()
const https = require('https')
const fs = require('fs')
const port = 3000
app.get('/', (req, res) => {
res.send('WORKING!')
})
// create certificate on ./security folder
const httpsOptions = {
key: fs.readFileSync('./security/key.pem'),
cert: fs.readFileSync('./security/cert.pem')
}
const server = https.createServer(httpsOptions, app).listen(port, () => {
console.log('server running at ' + port)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment