-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (46 loc) · 1.58 KB
/
app.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
59
60
61
"use strict"
const http = require("http");
const fs = require("fs");
const Router = require("./lib/router");
const Static = require("./lib/static");
const Socket = require("./lib/socket");
if (!fs.existsSync("./logs")) fs.mkdirSync("./logs");
Math.clamp = Math.clamp || ((x,l,h) => Math.max(l,Math.min(x,h)));
const PORT = Math.clamp(+process.env.PORT||8080, 1, 65535);
const HOST = "0.0.0.0";
const server = http.createServer();
const stat = new Static("./public");
const wss = new Socket(server);
const app = new Router();
function sj(res, data) { // For convenience
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(data));
}
app.get("/hist.json", (req, res) => sj(res, []));
//app.get("/(?<id>[0-9A-Z]+)", (req, res, path) => {
// res.setHeader("Content-Type", "text/plain");
// res.end(path[id]);
//});
//app.gpost("/(?<id>[0-9A-Z]+)/file", (req, res, path, data) => {
// res.end(data);
//}, 1<<28); // 256MB Max
//app.put("/file/(.+)", (req, res, path) => {
// req.pipe(fs.createWriteStream("./static/"+path[1]));
//});
wss.on("open", client => {
console.log(`${Date.now()} SOCK open`);
client.send("hello", {foo:"bar"});
});
wss.on("close", client => {
console.log(`${Date.now()} SOCK close`);
});
wss.on("world", (client, data) => {
console.log(`${Date.now()} SOCK world`, data);
wss.sendAll("yay", {meh:"woot"});
});
server.on("request", (req, res) => {
console.log(`${Date.now()} ${req.method} ${req.url}`);
app.route(req, res) || stat.route(req, res);
});
server.listen(PORT/*, HOST*/);
console.log(`${Date.now()} Running on http://${HOST}:${PORT}`);