/
index.js
59 lines (48 loc) · 1.92 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 {resolve, join} = require('path');
const {createServer} = require('https');
const express = require('express');
const fallback = require('express-history-api-fallback');
const {redirectToHTTPS} = require('express-http-to-https');
const certificate = require('devcert-san');
const pupperender = require('pupperender');
const loggerMiddleware = require('./lib/logger-middleware');
const log = require('./lib/log');
const getStHost = () => process.platform === 'win32' ? '127.0.0.1' : 'localhost';
module.exports = async (folder, options) => {
const opt = typeof options === 'object' ? {...options} : {};
const ROOT = resolve(folder || './');
const PORT = opt.p || opt.port || 8080;
const HOST = opt.h || opt.host || getStHost();
const FALLINDEX = opt.f || opt.fallback || 'index.html';
const DEBUG = opt.d || opt.debug || false;
const LOCALHTTPS = opt.s || opt.https || false;
const CACHE = opt.c || opt.cache || false;
const CACHE_TTL = opt.cacheTTL || 3600;
const SSL = opt.ssl || false;
const IS_DEV = process.env.NODE_ENV !== 'production';
const app = express();
// Don't redirect if the hostname is `localhost` or `127.0.0.1`
app.use(redirectToHTTPS(LOCALHTTPS ? [] : [/localhost|127.0.0.1/]));
app.use(loggerMiddleware(DEBUG));
app.use(pupperender.makeMiddleware({debug: DEBUG, useCache: CACHE, cacheTTL: CACHE_TTL}));
app.use(express.static(ROOT));
app.use(fallback(FALLINDEX, {root: ROOT}));
const sslCert = SSL && IS_DEV ?
await certificate.default('pwa-server', {installCertutil: true}) : null;
return new Promise(resolve => {
const server = sslCert ? createServer(sslCert, app) : app;
const res = server.listen(
PORT,
HOST,
() => {
log.green('HSP started 🤘');
log.info(
`Path: ${ROOT} \nHost: ${sslCert ? 'https' : 'http'}://${HOST}:${PORT} \nFalling on: ${join(ROOT, FALLINDEX)}`
);
log.yellow('Hit CTRL-C to stop the server');
resolve(res);
}
);
});
};