forked from scottcorgan/pushstate-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
const connect = require("connect");
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;
const DIRECTORY = "public";
const FILE = "index.html";
const HOST = "0.0.0.0";
exports.start = function(options, _onStarted) {
options = options || {};
let port = options.port || process.env.PORT || PORT;
let directory = options.directory || DIRECTORY;
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
directories.forEach(directory =>
app.use(serveStatic(directory, { extensions: ["html"] }))
);
// Then, serve the fallback file
app.use(serveStaticFile(path.join(directory, file)));
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;
}
};