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 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');


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!');



Aug 10, 2018 in Blockchain by slayer
• 29,040 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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();

.createServer(options, function (req, res) {
  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
• 27,620 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,620 points
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 26, 2018 in Blockchain by Shashank
• 10,330 points
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
• 16,450 points
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
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
• 27,620 points
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.