From d1a9552a4f14baf1f2b4f9f8ef5dea7c4f080b8e Mon Sep 17 00:00:00 2001 From: Pierrick Prudhomme Date: Tue, 23 Jan 2018 16:57:33 +0100 Subject: [PATCH 1/4] Add dependencies to https and fs. --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 5e11252..49c9cc9 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ "compression": "1.7.0", "connect": "3.6.2", "connect-static-file": "1.2.0", + "fs": "0.0.1-security", + "https": "1.0.0", "minimist": "1.2.0", "serve-static": "1.12.3" }, From 6b6909a1cc2428f973e6d90d939fe9188b44883b Mon Sep 17 00:00:00 2001 From: Pierrick Prudhomme Date: Tue, 23 Jan 2018 16:58:48 +0100 Subject: [PATCH 2/4] Add https support to the library. --- index.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ba6b4c9..0ac97e2 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const path = require("path"); const serveStatic = require("serve-static"); const serveStaticFile = require("connect-static-file"); const compression = require("compression"); +const fs = require("fs"); +const https = require("https"); const app = connect(); const PORT = 9000; @@ -20,8 +22,16 @@ exports.start = function(options, _onStarted) { let directories = options.directories || [directory]; let file = options.file || FILE; let host = options.host || HOST; + let useSSL = options.useSSL || false; let onStarted = _onStarted || function() {}; + let https_options = useSSL + ? { + key: fs.readFileSync(options.sslKeyPath), + cert: fs.readFileSync(options.sslCertPath) + } + : null; + app.use(compression()); // First, check the file system @@ -32,9 +42,17 @@ exports.start = function(options, _onStarted) { // Then, serve the fallback file app.use(serveStaticFile(path.join(directory, file))); - const server = app.listen(port, host, err => - onStarted(err, server.address()) - ); + if (useSSL) { + const server = https + .createServer(https_options, app) + .listen(port, host, err => onStarted(err, server.address())); + + return server; + } else { + const server = app.listen(port, host, err => + onStarted(err, server.address()) + ); - return server; + return server; + } }; From 25cd79f2582be768772182e33091d9abdb80531b Mon Sep 17 00:00:00 2001 From: Pierrick Prudhomme Date: Tue, 23 Jan 2018 16:59:04 +0100 Subject: [PATCH 3/4] Add https support to the executable. --- bin/pushstate-server | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bin/pushstate-server b/bin/pushstate-server index 159940c..c7818bf 100755 --- a/bin/pushstate-server +++ b/bin/pushstate-server @@ -2,6 +2,7 @@ const server = require('../index') const argv = require('minimist')(process.argv.slice(2)) +const fs = require('fs') if (argv.h || argv.help) { console.log([ @@ -11,15 +12,39 @@ if (argv.h || argv.help) { ' -d Directory to serve [public]', ' -p Port to use [9000]', ' -f Custom file to serve [index.html]', + ' -s Serve files trough https', + ' -c --cert Path to SSL certificate', + ' -k --key Path to SSL private key', ' -h --help Print this list and exit.' ].join('\n')); process.exit(); } +if (argv.s) { + if (!argv.c) { + console.log('Missing SSL certificate path !') + process.exit() + } else if(!fs.existsSync(argv.c)) { + console.log('Invalid SSL certificate path !') + process.exit() + } + + if (!argv.k) { + console.log('Missing SSL private key path !') + process.exit() + } else if(!fs.existsSync(argv.k)) { + console.log('Invalid SSL key path !') + process.exit() + } +} + server.start({ directory: argv.d, port: argv.p, - file: argv.f + file: argv.f, + useSSL: argv.s, + sslCertPath: argv.c, + sslKeyPath: argv.k }, (err, address) => - console.log(`Listening on port ${address.port} (http://${address.address}:${address.port})`) + console.log(`Listening on port ${address.port} (${argv.s ? 'https' : 'http'}://${address.address}:${address.port})`) ) From f318949cb601509dd5f701d376e04157115ebb63 Mon Sep 17 00:00:00 2001 From: Pierrick Prudhomme Date: Wed, 24 Jan 2018 09:01:52 +0100 Subject: [PATCH 4/4] Update README to include https usage. --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 87d54b2..3b1d1e4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,20 @@ server.start({ }); ``` +or for https connection + +```js +var server = require('pushstate-server'); + +server.start({ + port: 4200, + directory: './public', + useSSL: true, + sslKeyPath: './server.key', + sslCertPath: './server.crt' +}); +``` + or for multiple directories ```js @@ -50,7 +64,7 @@ npm install -g pushstate-server ``` ``` -usage: pushstate-server [-d directory] [-p port] [-f file] +usage: pushstate-server [-d directory] [-p port] [-f file] [-s -c path/to/server.crt -k path/to/server.key] ``` ## API @@ -69,3 +83,10 @@ usage: pushstate-server [-d directory] [-p port] [-f file] * `file` * Custom file to serve * defaults to `index.html` +* `useSSL` + * Let the server use HTTPS instead of HTTP + * defaults to `false` +* `sslKeyPath` + * Path for the SSL private key +* `sslCertPath` + * Path for the SSL certificate