Skip to content

Instantly share code, notes, and snippets.

@scsskid
Forked from bencentra/server.js
Created December 9, 2020 15:13
Show Gist options
  • Save scsskid/136c5ac1445fce725fc8c8728c0e24e5 to your computer and use it in GitHub Desktop.
Save scsskid/136c5ac1445fce725fc8c8728c0e24e5 to your computer and use it in GitHub Desktop.

Revisions

  1. @bencentra bencentra created this gist Feb 13, 2017.
    39 changes: 39 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    /*
    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);