HTTPS server on node.js not working

I am trying to create an HTTPS server. I created privatekey.pem and certicate.pem with Cygwin64, following the following instructions:

openssl genrsa -out privatekey.pem 1024
openssl req -new -key privatekey.pem -out certrequest.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

while my server code is:

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

var options = {
  key: fs.readFileSync('privatekey.pem'),
  cert: fs.readFileSync('certificate.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8001);

When I try to connect to the address https://localhost:8001, the webpage is not available. Can anybody help me?

+4
source share
1 answer

Your code is completely functional. I tested it in my local host. I think the problem is with your key generation. Try creating the keys again and starting your server. I hope this works.

: , , localhost. - IDE (, codio.com, koding.com, cloud9.com ..). , SSL , Verysign.

.

1: "key.pem" .

$ openssl genrsa 1024 > key.pem

2: SSL "key-cert.pem" .

$ openssl req -x509 -new -key key.pem > key-cert.pem

3: 'server.js .

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

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

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8001);

4: , .

$ node server.js

5: URL 'https://localhost:8001' , "hello world" .

+4

Source: https://habr.com/ru/post/1546876/


All Articles