Skip to content

Instantly share code, notes, and snippets.

@ashleydavis
Forked from bencentra/server.js
Created December 7, 2018 00:17
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 ashleydavis/56f2a82859a066f8a02aa22c69678e0c to your computer and use it in GitHub Desktop.
Save ashleydavis/56f2a82859a066f8a02aa22c69678e0c to your computer and use it in GitHub Desktop.
An HTTPS server for static content (Node.js)
/*
This module creates an HTTPS web server and serves static content
from a specified directory on a specified port.
To generate a new cert:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
To remove the passphrase requirement:
openssl rsa -in key.pem -out newkey.pem && mv newkey.pem key.pem
Or just include the "passphrase" option when configuring the HTTPS server.
Sources:
- http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/
- https://expressjs.com/en/starter/static-files.html
*/
const fs = require('fs');
const https = require('https');
const express = require('express');
const app = express();
app.use(express.static(process.env.SERVE_DIRECTORY || 'dist'));
app.get('/', function(req, res) {
return res.end('<p>This server serves up static files.</p>');
});
const options = {
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('cert.pem', 'utf8'),
passphrase: process.env.HTTPS_PASSPHRASE || ''
};
const server = https.createServer(options, app);
server.listen(process.env.SERVER_PORT || 8443);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment