-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (47 loc) · 1.58 KB
/
server.js
File metadata and controls
54 lines (47 loc) · 1.58 KB
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
const path = require("path");
const fs = require("fs");
const untildify = require("untildify");
const express = require("express");
const https = require("https");
const argv = require("yargs")
.usage(
"Usage: $0 --appno [appno] --key [key] --cert [cert] --port [port] --sw-url [Service Worker URL] --sw-path [Service Worker Path]"
)
.demandOption(["appno", "key", "cert"]).argv;
const app = express();
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.engine("js", require("ejs").renderFile);
const port = argv["port"] || process.env.PORT || 3000;
const options = {
key: fs.readFileSync(path.resolve(untildify(argv["key"]))),
cert: fs.readFileSync(path.resolve(untildify(argv["cert"]))),
};
const server = https.createServer(options, app);
const params = {
appno: argv["appno"],
sw: argv["sw-url"] || "https://aldebaran.push7.com/ex-push7-worker.js",
};
app.get("/", function (req, res) {
res.render(path.resolve(__dirname, "./src/index.html"), {
...params,
host: req.get("host"),
});
});
app.get("/push7-worker.js", function (req, res) {
res.type("text/javascript");
if (!!argv["sw-path"]) {
// sw-path が設定されている場合はそちらを優先する
res.sendFile(untildify(argv["sw-path"]));
return;
}
res.render(
path.resolve(__dirname, "./src/push7-worker.js"),
{ ...params, host: req.get("host") },
(error, html) => {
res.send(Buffer.from(html));
}
);
});
app.use("/", express.static(path.resolve(__dirname, "./src")));
server.listen(port, () => console.log(`app listening on port ${port}`));