-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
40 lines (31 loc) · 1.04 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
"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 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 onStarted = _onStarted || function() {};
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)));
const server = app.listen(port, host, err =>
onStarted(err, server.address())
);
return server;
};