-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
176 lines (154 loc) · 5.62 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Made by: Jiab77 <https://github.com/Jiab77>
Based on:
- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework
- https://blog.bloomca.me/2018/12/22/writing-a-web-server-node.html
- https://nodejs.org/en/docs/guides/getting-started-guide/
- https://www.w3schools.com/nodejs/nodejs_http.asp
*/
"use strict";
// Dependencies
const http = require('http');
const fs = require('fs');
const path = require('path');
const process = require('process');
// Config
const documentRoot = '.';
const debugMode = false;
const enableDirectoryListing = false;
const enableUrlDecoding = false;
const hostname = process.env.NODE_WEB_HOST || '127.0.0.1';
const port = process.env.NODE_WEB_PORT || 8001;
// Directory listing function
// Improved version of: https://stackoverflow.com/a/31831122
var directoryListing = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (!pending) {
return done(null, {name: path.basename(dir), type: 'folder', children: results});
}
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
directoryListing(file, function(err, res) {
results.push({
type: 'folder',
name: path.basename(file),
time: stat.mtime,
size: stat.size,
children: res
});
if (!--pending) {
done(null, results);
}
});
}
else {
results.push({
type: 'file',
name: path.basename(file),
size: stat.size,
time: stat.mtime
});
if (!--pending) {
done(null, results);
}
}
});
});
});
};
// Web server
http.createServer(function (request, response) {
const url = request.url;
console.log('[Info] Requested:', url);
if (debugMode === true && enableUrlDecoding === true) {
console.log('[Debug] Decoded:', decodeURI(url));
}
var filePath = url;
// Correct root path
if (filePath === '/') {
filePath = documentRoot + '/index.html';
}
else {
filePath = documentRoot + (enableUrlDecoding === true ? decodeURI(url) : url);
}
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp3': 'audio/mp3',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
// Serve static files
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code === 'ENOENT') {
fs.readFile(documentRoot + '/404.html', function(error, content) {
if (error) {
console.error(error);
}
else {
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
// log served 404 page
console.log('[Info] Served 404 page.');
}
});
}
else if (error.code === 'EISDIR' && enableDirectoryListing === true) {
directoryListing(filePath, function(err, res) {
if(err) {
console.error(err);
}
// log directory content
if (debugMode === true) {
console.log('[Info] Served as JSON:', url);
console.log('[Debug] Encoded:', JSON.stringify(res));
}
else {
// log served response
console.log('[Info] Served as JSON:', url);
}
// return directory content as JSON
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(res), 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ...\n');
// display error
console.log('[Error] Could not serve request:', url);
console.error(error);
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
// log served response
console.log('[Info] Served:', url);
}
});
}).listen(port, hostname, () => {
console.log(`NodeJS Development Server (http://${hostname}:${port}) started`);
});