How to enable SSL/TLS for Express.js application

For node.js applications, you can enable SSL on the application side or using Nginx or Apache running as a reverse proxy. To enable SSL for a node.js express application, use the following code.

const fs = require('fs')
const https = require('https')
const express = require('express')

var port = 3000;

var options = {
    key: fs.readFileSync('./ssl/ssl.key'),
    cert: fs.readFileSync('./ssl/ssl.crt'),
};

var app = express();

var server = https.createServer(options, app).listen(port, function(){
  console.log("Express server listening on port " + port);
});

app.get('/', function (req, res) {
    res.writeHead(200);
    res.end("hello world\n");
});
Add a comment

Leave a Reply

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

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement