Enable HTTPS in Node.js Express Applications

In this tutorial, we will learn how to enable HTTPS in your Node.js Express applications to protect your users' data and provide a more secure experience. If you want to enable HTTPS in your node js express application. For this, you need an ssl certificate. If you have added the SSL certificate files to the server. So now you can see in the code given below. How to Enable HTTPS in Node Js Express App?


Now open your node js express application server.js file or app.js file and then add privateKey and Certificate to enable HTTPS in node js express app; as shown below:

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 here

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(3000);
httpsServer.listen(4000);

Run the app with command, npm start.

For https = https://localhost:4000/

For without https = https://localhost:3000/


In this tutorial you learned how to enable https in node js express application.

Thanks for reading!!!

Enable HTTPS in Node.js Express Applications
5 Likes3.90 GEEK