-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.js
78 lines (69 loc) · 2.18 KB
/
requests.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
var url = require('url');
var fs = require("fs");
var path = require('path')
// thanks! http://blog.kevinchisholm.com/javascript/node-js/making-a-simple-http-server-with-node-js-part-iv/
var headers = {
"access-control-allow-origin": "*",
"access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
"access-control-allow-headers": "content-type, accept",
"access-control-max-age": 10, // Seconds.
'Content-Type': "text/html"
};
var extensions = {
".html" : "text/html",
".css" : "text/css",
".js" : "application/javascript",
".png" : "image/png",
".gif" : "text/gif",
".jpg" : "text/jpg",
".ico" : "text/ico"
};
function getFile(filePath,res,page404,mimeType){
//does the requested file exist?
console.log('looking here: ',filePath)
fs.exists(filePath,function(exists){
//if it does...
if(exists){
//read the fiule, run the anonymous function
fs.readFile(filePath,function(err,contents){
if(!err){
//if there was no error
//send the contents with the default 200/ok header
res.writeHead(200,{
"Content-type" : mimeType,
"Content-Length" : contents.length
});
res.end(contents);
} else {
//for our own troubleshooting
console.dir(err);
};
});
} else {
//if the requested file was not found
//serve-up our custom 404 page
fs.readFile(page404,function(err,contents){
//if there was no error
if(!err){
//send the contents with a 404/not found header
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(contents);
} else {
//for our own troubleshooting
console.dir(err);
};
});
};
});
};
function handleRequest(req, res) {
var
fileName = req.url,
ext = path.extname(fileName),
localFolder = __dirname + '/public',
page404 = localFolder + '/404.html';
if (fileName === '/') {fileName ='/index.html'}
console.log("Serving request type " + req.method + " for url " + req.url , localFolder, '\n', fileName);
getFile((localFolder + fileName),res,page404,extensions[ext]);
};
exports.handleRequest = handleRequest;