简体   繁体   中英

Create an HTTPS server with Node.js

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:

在此处输入图片说明

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

try with:

openssl genrsa -out key.pem 1024

or

openssl genrsa -out key.pem 2048

A key size of 512 is considered weak

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM