-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (30 loc) · 978 Bytes
/
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
var http = require("http");
var parse = require("url").parse;
var config = require("./config");
var fileHandler = require("./filehandler");
var types = config.types;
var rootFolder = config.rootFolder;
var defaultIndex = config.defaultIndex;
var server = http.createServer();
module.exports = server;
server.on("request",onRequest);
function onRequest(request, response){
var filename = parse(request.url).pathname;
console.log(filename);
console.log("-------")
if(filename === '/'){
filename = defaultIndex;
}
var fullPath = rootFolder + filename;
var extension = filename.substring(filename.lastIndexOf(".") + 1);
fileHandler(fullPath, function(data){
response.writeHead(200, {
'Content-Type': types[extension] || 'text/plain',
'Content-Length': data.length
});
response.end(data);
}, function(error){
response.writeHead(404);
response.end();
});
}