Node.js Express Enable HTTPS Tutorial

Node.js Express Enable HTTPS Tutorial

In this tutorial, you will learn how to enable HTTPS in Node js + express applications using SSL certificate files.

Enable HTTPS in Node js Express

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/

Conclusion

Enable HTTPS in node js express app tutorial, you have learned how to enable https in node js express applications.

Recommended Node js Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *