-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-www.js
161 lines (130 loc) · 3.7 KB
/
app-www.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(() => {
"use strict";
//============================
const __ = require("timeengine");
const _ = require("immutable");
const port_www = 3000;
const port_op = 2950;
const wwwDir = "www/";
const http = require("http");
const url = require("url");
const path = require("path");
const fs = require("fs");
const mimeTypes = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"svg": "image/svg+xml",
"ico": "image/vnd.microsoft.icon"
// more
};
__.log.t = "app.js started!";
const wwwObj = __();
const wwwLoad = (wwwDir) => {
__.log.t = "==== wwwLoad ===";
const wwwObj = __();
wwwObj.t = {};
const __runSeek = __
.intervalSeq(_.Seq.of(true), 0)
.__(() => {
seekDir(wwwDir);
});
const seekDir = (dir) => {
__.log.t = "seekDir:" + dir;
fs.readdir(dir, (err, dirA) => { //readdir error indicates a file
if (err) {
const uri = dir.split(wwwDir)[1];
const key = path.join("/", uri);
console.log("key:" + key); //under wwwDir
const file = dir;
fs.readFile(file, (err, data) => {
if (err) {
__.log.t = "fileLoadError!";
} else {
wwwObj.t = addObj(wwwObj.t, key, data);
}
});
} else {
const dummy = dirA.map((dirOrFile) => {
seekDir(path.join(dir, dirOrFile));
});
}
});
};
const addObj = (baseObj, newIdx, newEl) => {
baseObj[newIdx] = newEl;
return baseObj;
};
return wwwObj.t;
};
//======================================================
const request = (req, res) => {
const writeOut = (contentKey) => {
res
.writeHead(200, {
"Content-Type": mimeTypes[path.extname(contentKey).split(".")[1]]
});
const content = wwwObj.t[contentKey];
res.end(content);
return;
};
const uri = url.parse(req.url).pathname;
__.log.t = "uri:" + uri;
if (uri.split("/")[1] === "i") {
__.log.t = "item page requested!";
} else if (!wwwObj.t[uri]) {
__.log.t = "no-requestedfile -> index.html";
writeOut("/index.html");
} else {
__.log.t = "file requested -> wrieteout";
writeOut(uri);
}
return;
};
const serverUp = () => {
console.info("HTTP server listening", port_www);
};
const __runNow = __
.intervalSeq(_.Seq.of(true), 0)
.__(() => {
__.log.t = "wwwLoading";
wwwObj.t = wwwLoad(__dirname + "/" + wwwDir);
});
const __delay = __
.intervalSeq(_.Seq.of(true), 500)
.__(() => {
//=========================================
__.log.t = "www server starting";
// socket to db app-op/js
const db = require("socket.io-client")("http://localhost:" + port_op);
db
.on("connect", () => {
__.log.t = "#####################app-op connected";
})
.on("event", (data) => {
__.log.t = "some db event";
})
.on("disconnect", () => {
__.log.t = "#####################app-op disconnected";
});
//www server
const wwwserver = http
.createServer(request)
.listen(port_www, serverUp);
const io = require("socket.io")(wwwserver);
io
.on("connection", (socket) => {
__.log.t = "a site-viewer connected";
socket
.on("disconnect", () => {
__.log.t = "a site-viewer disconnected";
});
});
//=========================================
});
//============================
})();