-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.js
More file actions
198 lines (165 loc) · 5.32 KB
/
Copy pathwebServer.js
File metadata and controls
198 lines (165 loc) · 5.32 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"use strict";
var http = require('http');
var fs = require('fs');
var path = require('path');
/**
* webServer.js
*
* Static Node Web Server.
*
* NODE.JS - Resources
* node.js - http://nodejs.org/
* MIME types - "http://www.freeformatter.com/mime-types-list.html"
* HTTP status codes - "http://en.wikipedia.org/wiki/List_of_HTTP_status_codes"
*/
// Server settings
// (defaults to heroku compatible settings [curruen IP & evironment PORT])
var IP;
var PORT = process.env.PORT || 5000;
var SITE_ROOT = 'www';
var _INDEX = '/index.html';
var _404 = '/404.html';
// escape codes
var magenta = '\u001b[35m';
var green = '\u001b[32m';
var red = '\u001b[31m';
var reset = '\u001b[0m';
// normalize the name of the sites root folder
SITE_ROOT = './'+ SITE_ROOT;
var redirectLoop = 0;
// create a new server instance
var server = http.createServer(function (request, response) {
// Set the filepath
var filePath = SITE_ROOT + (path.normalize(request.url)).toLowerCase();
// Load index.html if no file path is set
if (filePath == SITE_ROOT+'\\') filePath = SITE_ROOT + _INDEX;
// Resolve MIME types
var extname = path.extname(filePath);
var contentType;
switch (extname) {
case '.html':
contentType = 'text/html';
break;
case '.htm':
contentType = 'text/html';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.jpg':
contentType = 'image/jpeg';
break;
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.png':
contentType = 'image/png';
break;
case '.gif':
contentType = 'image/gif';
break;
case '.svg':
contentType = 'image/svg+xml';
break;
case '.ico':
contentType = 'image/x-icon';
break;
case '.ttf':
contentType = 'application/x-font-ttf';
break;
case '.eot':
contentType = 'application/vnd.ms-fontobject';
break;
case '.woff':
contentType = 'application/x-font-woff';
break;
case '.otf':
contentType = 'application/x-font-otf';
break;
case '.mp4':
contentType = 'video/mp4';
break;
case '.ogv':
contentType = 'video/ogg';
break;
case '.json':
contentType = 'application/json';
break;
case '.swf':
contentType = 'application/x-shockwave-flash';
break;
case '.zip':
contentType = 'application/zip';
break;
case '.rar':
contentType = 'application/x-rar-compressed';
break;
case '.pdf':
contentType = 'application/pdf';
break;
case '.docx':
contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case '.bin':
contentType = 'application/octet-stream';
break;
default:
contentType = 'text/plain';
}
// Load the requested file
fs.exists(filePath, function (exists) {
if (exists) {
fs.readFile(filePath, function (error, content) {
if (error) {
// 500 - Internal server error
response.writeHead(500);
response.end();
} else {
// 200 - OK
response.writeHead(200, { 'Content-Type': contentType});
response.end(content, 'utf-8');
}
redirectLoop = 0;
});
} else {
// Check if the request accepts a 'text/html' response. If not, return a 404 header.
var regex = new RegExp("text/html");
var rhc = request.headers.accept;
var acceptHTML = rhc.match(regex);
if(!acceptHTML) {
response.writeHead(404);
response.end();
return;
}
// 404 - Page not found, redirect loop
switch (redirectLoop) {
case 0:
response.writeHead(302, {'Location': _404});
break;
case 1:
response.writeHead(302, {'Location': _INDEX});
break;
default:
// 404 - Not Found
response.writeHead(404);
break;
}
response.end();
redirectLoop++;
}
});
});
exports.start = function (ip, port) {
if(ip) IP = ip;
if(port) PORT = port;
server.listen(PORT, IP);
if(ip) {
console.log(magenta + 'Server running at: ' + reset + 'http://' + IP + ':' + PORT + '/');
}else{
console.log(magenta + 'Server running at: ' + reset + 'http://localhost:' + PORT + '/');
}
return server;
}