Skip to content

Instantly share code, notes, and snippets.

@benfluleck
Last active May 5, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfluleck/7def82b900b6dffebe202a45b39390cd to your computer and use it in GitHub Desktop.
Save benfluleck/7def82b900b6dffebe202a45b39390cd to your computer and use it in GitHub Desktop.
HTTPS node server
var express = require('express');
var app = express();
var fs = require('fs');
var https = require('https');
var key = fs.readFileSync('key.pem');
var cert = fs.readFileSync( 'cert.pem' );
var options = {
key: key,
cert: cert,
passphrase: 'benny'
};
app.use('/',(req, res) => {
res.writeHead(200);
res.end('hello world\n');
});
https.createServer(options, app).listen(443);
@benfluleck
Copy link
Author

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

@benfluleck
Copy link
Author

To be able to serve a site on HTTPS from localhost you need to create a self-signed certificate.

A self-signed certificate will be enough to establish a secure HTTPS connection. For development purposes, I am using a self-signed certificate to achieve this.

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