How to add https security for Node js

0 votes

I am writing a chaincode and I want all the services in this to be https secure. I have followed https://www.linuxhelp.com/how-to-install-and-update-openssl-on-ubuntu-16-04/ to generate certificate and private key. Now I have this following code and I want to know how to add https security.

#!/usr/bin/env node

var app = require('../app');

var fs = require('fs');

var http = require('http');

var https = require('https');

require("dotenv").config();

var privateKey = fs.readFileSync('key.pem').toString();

var certificate = fs.readFileSync('cert.pem').toString();

var port = normalizePort(process.env.PORT || '8080');

app.set('port', port);

var hostname = process.env.HOSTNAME;

function normalizePort(val) {

  var port = parseInt(val, 10);

  if (isNaN(port)) {

    // named pipe

    return val;

  }

  if (port >= 0) {

    // port number

    return port;

  }

  return false;

}

https.createServer(function (req, res) {

  res.writeHead(200, {'Content-Type': 'text/plain'});

  res.write('Hello World!');

  res.end();

}).listen(8080);

Aug 10, 2018 in Blockchain by slayer
• 29,350 points
515 views

1 answer to this question.

0 votes

You need to add the key and cert to the createServer function.

const options = {
    key: fs.readFileSync('key.pem').toString();
    cert: fs.readFileSync('cert.pem').toString();
}


https
.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
})
.listen(443, function(){
    console.log("Server listening on localhost:443");
});

This will make your services https secure.

answered Aug 10, 2018 by digger
• 26,740 points

Related Questions In Blockchain

+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
939 views
0 votes
1 answer

How to create tokens in smart contracts from node.js?

Any modification to the Ethereum Blockchain will ...READ MORE

answered Jun 27, 2018 in Blockchain by Shashank
• 10,400 points
1,380 views
0 votes
1 answer
+1 vote
1 answer

Hyperledger - How can I use transaction certificates to enforce privacy?

The transaction certificates don't actually address privacy ...READ MORE

answered Apr 17, 2018 in Blockchain by Perry
• 17,100 points
659 views
0 votes
1 answer

Hyperledger Composer rest server NPM versions

The Hyperledger Composer pre-requisites can be installed ...READ MORE

answered Jul 13, 2018 in Blockchain by Christine
• 15,790 points
626 views
0 votes
1 answer

How to handle Overlapping of array objects in node js

Try this code, may be it works for ...READ MORE

answered Sep 18, 2018 in Blockchain by digger
• 26,740 points
605 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP