logo

The next-generation blog, news, and article for you to start sharing your stories today!

Node js Create HTTPS Server

Node Js HTTPS Server.

Hello friends today we are going to learn a simple code about how to start or create a simple https server in node js without any framework or library.

for run a secure server in like https in node js you need a self certified secure key.  SO you can generate those key by openssl.

Here are we https://www.openssl.org/

See the example for linux openssl obtain a new certificate.

Configure and start the server

// Import the https node module
let https = require('https');

// Instiate the server
let serverOptions = {
    'key' : '../key-path/key.pem',
    'cert' : '../key-path/cert.pem'
};
const server  = https.createServer(serverOptions, function(req, res) {
    // Handle your req and response here
    res.send('Welcome to keyscript');    
});

// Listen the server
server.listen(3001, function() {
    console.log('Https server is listing on port 3001');
});

So by using this simple code you can run your sever in secure mode.

Hello friends, I am vikram parihar, If you find this tutorial as useful please share the article and feeback by comment.

avatar

Vikram Parihar

An editor at Keyscript

Vikram Parihar is an senior software engineer. He is working since 2013 in web development technologies. He works in NodeJS, Angular, Vue, PHP, Laravel, Express JS and various popular technologies.