Enabling HTTPS on express.js
In express.js (since version 3) you should use that syntax:
var fs = require('fs');var http = require('http');var https = require('https');var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');var credentials = {key: privateKey, cert: certificate};var express = require('express');var app = express();// your express configuration herevar httpServer = http.createServer(app);var httpsServer = https.createServer(credentials, app);httpServer.listen(8080);httpsServer.listen(8443);
In that way you provide express middleware to the native http/https server
If you want your app running on ports below 1024, you will need to use sudo
command (not recommended) or use a reverse proxy (e.g. nginx, haproxy).
First, you need to create selfsigned.key and selfsigned.crt files. Go to Create a Self-Signed SSL Certificate Or do following steps.
Go to the terminal and run the following command.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./selfsigned.key -out selfsigned.crt
- After that put the following information
- Country Name (2 letter code) [AU]: US
- State or Province Name (full name) [Some-State]: NY
- Locality Name (eg, city) []:NY
- Organization Name (eg, company) [Internet Widgits Pty Ltd]: xyz (Your - Organization)
- Organizational Unit Name (eg, section) []: xyz (Your Unit Name)
- Common Name (e.g. server FQDN or YOUR name) []: www.xyz.com (Your URL)
- Email Address []: Your email
After creation adds key & cert file in your code, and pass the options to the server.
const express = require('express');const https = require('https');const fs = require('fs');const port = 3000;var key = fs.readFileSync(__dirname + '/../certs/selfsigned.key');var cert = fs.readFileSync(__dirname + '/../certs/selfsigned.crt');var options = { key: key, cert: cert};app = express()app.get('/', (req, res) => { res.send('Now using https..');});var server = https.createServer(options, app);server.listen(port, () => { console.log("server starting on port : " + port)});
- Finally run your application using https.
More information https://github.com/sagardere/set-up-SSL-in-nodejs
I ran into a similar issue with getting SSL to work on a port other than port 443. In my case I had a bundle certificate as well as a certificate and a key. The bundle certificate is a file that holds multiple certificates, node requires that you break those certificates into separate elements of an array.
var express = require('express'); var https = require('https'); var fs = require('fs'); var options = { ca: [fs.readFileSync(PATH_TO_BUNDLE_CERT_1), fs.readFileSync(PATH_TO_BUNDLE_CERT_2)], cert: fs.readFileSync(PATH_TO_CERT), key: fs.readFileSync(PATH_TO_KEY) }; app = express() app.get('/', function(req,res) { res.send('hello'); }); var server = https.createServer(options, app); server.listen(8001, function(){ console.log("server running at https://IP_ADDRESS:8001/") });
In app.js you need to specify https and create the server accordingly. Also, make sure that the port you're trying to use is actually allowing inbound traffic.