-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (116 loc) · 3.24 KB
/
server.js
File metadata and controls
128 lines (116 loc) · 3.24 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
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
// bem130
const http = require("http");
const fs = require('fs');
const url = require('url');
const port = 80;
const server = http.createServer(server_request);
server.listen(port);
var NML = require('./nml.js');
nml = {};
nml.html = (new TextDecoder("utf-8")).decode(new Uint8Array(fs.readFileSync("./nml.html")));
nml.css = (new TextDecoder("utf-8")).decode(new Uint8Array(fs.readFileSync("./nml.css")));
nml.paleblue = (new TextDecoder("utf-8")).decode(new Uint8Array(fs.readFileSync("./nml.paleblue.css")));
let mime = {
"txt":"text/plain; charset=UTF-8",
"nml":"text/nml; charset=UTF-8",
"html":"text/html; charset=UTF-8",
"css":"text/css; charset=UTF-8",
"nml":"text/neknajml; charset=UTF-8",
"js":"text/javascript",
"png":"image/png",
"jpg":"image/jpeg",
"jpeg":"image/jpeg",
"gif":"image/gif",
};
function NmlRes(data,mode="html") {
switch (mode) {
case "html":
let rt = new NML.NMLc((new TextDecoder("utf-8")).decode(new Uint8Array(data)));
let tnml = rt.getHTMLtext();
let html = nml.html.replace("[nmlbody]",tnml).replace("/*nmlstyle*/",nml.css).replace("/*nmltheme*/",nml.paleblue);
return [html,mime.html];
break;
case "nml":
return [data,mime.nml];
break;
case "txt":
return [data,mime.txt];
break;
}
}
function server_request(req, response) {
let head = {code:200,type:"text/plain; charset=UTF-8",filetype:"txt"};
let res = "";
//
// transfer
//
let reqparse = url.parse(req.url);
switch (reqparse.pathname) {
case "/404":
case "/404.nml":
head.code = 404;
break;
case "/": // top page
reqparse.pathname = "/index.nml";
break;
}
//
// end
//
//
// get file
//
if (fs.existsSync("data"+reqparse.pathname)) {
res = fs.readFileSync("data"+reqparse.pathname);
}
else if (fs.existsSync("data"+reqparse.pathname+".nml")) {
res = fs.readFileSync("data"+reqparse.pathname+".nml");
reqparse.pathname += ".nml";
}
else {
res = fs.readFileSync("data/404.nml");
reqparse.pathname += ".nml";
head.code = 404;
}
//
// end
//
//
// set MIME
//
let fnsplit = reqparse.pathname.split(".");
head.filetype = fnsplit[fnsplit.length-1];
if (mime[head.filetype]!=null) {
head.type = mime[head.filetype];
}
//
// end
//
//
// NML
//
if (head.filetype=="nml") {
console.log("nml")
let nml_ = NmlRes(res);
res = nml_[0]; head.type = nml_[1];
}
//
// end
//
//
// response
//
response.writeHead(head.code, {"Content-Type": head.type});
response.end(res);
//
// end
//
//
// push log
//
console.log(`\x1b[33m${(new Date()).toString()}\x1b[39m requrl: \x1b[34m${req.url}\x1b[39m path: \x1b[34m${reqparse.path}\x1b[39m response: \x1b[36m${head.code}\x1b[39m res: \x1b[35m${reqparse.pathname}\x1b[39m`);
//
// end
//
}
console.log(`The server has started: ${port}`);