Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to launch server trough HTTPS #77

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
29 changes: 27 additions & 2 deletions bin/pushstate-server
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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})`)
)
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
}
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"compression": "1.7.3",
"connect": "3.6.6",
"connect-static-file": "2.0.0",
"fs": "0.0.1-security",
"https": "1.0.0",
"minimist": "1.2.0",
"serve-static": "1.13.2"
},
Expand Down