Skip to content

Instantly share code, notes, and snippets.

@MacKentoch
Created July 15, 2017 18:52
Show Gist options
  • Select an option

  • Save MacKentoch/90f06360709275b6565f2296ffc78a1c to your computer and use it in GitHub Desktop.

Select an option

Save MacKentoch/90f06360709275b6565f2296ffc78a1c to your computer and use it in GitHub Desktop.

How to create an https server?

1. generate a self-signed certificate, run the following in your shell:

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
rm csr.pem

This should leave you with two files, cert.pem (the certificate) and key.pem (the private key). This is all you need for a SSL connection.

2. So now you set up a quick hello world example (the biggest difference between https and http is the options parameter):

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

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

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

Now in your browser go to the url: https://localhost:8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment