2

I'm getting error while trying to access HTTPS server I created with NodeJS.

What do I do :

1.First of all : generate a self-signed certificate :

openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem

2.Create simple NodeJs Https server -

var https = require('https');
var fs = require('fs');
var server_port = 8080;

var httpsOptions = {
    key: fs.readFileSync('\key.pem'),
    cert: fs.readFileSync('\cert.pem')
};

var app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}   
https.createServer(httpsOptions, app).listen(server_port);
  1. run the server : node main.js (the file name) The server is running but when I try access https://localhost:8080

  2. Connect to the server. The result is:

enter image description here

jww
  • 83,594
  • 69
  • 338
  • 732
hcohen
  • 195
  • 4
  • 16
  • 1
    Since the server is self signed, the browser will cry. You will need to add the cert to the trust store of the browser. – Nehal J Wani Sep 18 '16 at 14:15
  • @NehalJWani does this still apply for a production server? I'm curious about self-signing certs – sova Sep 18 '16 at 17:09
  • 1
    @sova For public facing production servers, you have to either buys certificates or, get free ones from letsencrypt or startcom. – Nehal J Wani Sep 18 '16 at 18:09
  • @Nehal J Wani : And how do I do that ? – hcohen Sep 18 '16 at 19:34
  • 1
    The certificate is probably malformed since its being used in a browser. Also see [How do you sign Certificate Signing Request with your Certification Authority](http://stackoverflow.com/a/21340898/608639) and [How to create a self-signed certificate with openssl?](http://stackoverflow.com/q/10175812/608639) It provides a lot of background information on X.509 server certificates, how to present names, and where the various rules come from. – jww Sep 18 '16 at 22:33
  • 1
    Also see [How to create an HTTPS server in Node.js?](http://stackoverflow.com/q/5998694), [Create HTTPS server with node js](http://stackoverflow.com/q/16610612). I'm guessing the answers have already been provided to you, except for the certificate hostnames. – jww Sep 18 '16 at 22:35
  • Thanks @jww I solved the issue – hcohen Sep 19 '16 at 14:44

2 Answers2

2

Probably this port used by another application (e.g skype). Try another port.

2

try with:

openssl genrsa -out key.pem 1024

or

openssl genrsa -out key.pem 2048

A key size of 512 is considered weak

hex7c0
  • 61
  • 3
  • 5