-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpServer.js
33 lines (30 loc) · 913 Bytes
/
httpServer.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
var httpServer = require('http').createServer(handler)
,fs = require('fs')
,url = require('url');
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
function handler (request, response) {
var pathname = url.parse(request.url).pathname;
//console.log('pathname: ' + pathname);
if ('/' === pathname) {
pathname = '/index.html';
}
if (pathname.endsWith('.html') || pathname.endsWith('.js') || pathname.endsWith('.css')) {
fs.readFile(__dirname + pathname, function (error, data) {
if (error) {
response.writeHead(404);
return response.end('Resource not found');
}
response.writeHead(200);
response.end(data);
});
} else {
response.writeHead(404);
response.end('Resource not found');
}
};
exports.start = function() {
httpServer.listen(80);
return httpServer;
};