forked from tamathar/webGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandler.js
73 lines (62 loc) · 1.82 KB
/
requestHandler.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
/*******************************************************************
* file: requestHandlers.js
* Description: Handles requests for specific URLs for the server
* Author: Tam
*******************************************************************/
//required modules
var url = require("url");
var querystring = require("querystring");
var fs = require("fs");
var parseObject = require("./parseObj").parseObj;
//helper function to serve a requested file (for abstraction purposes)
function serveFile(response, path, type)
{
console.log("serving " + path + " ...");
fs.readFile(path, function(error, file)
{
if(error)
{
console.log(__dirname);
console.log(error);
response.writeHead(500, {"Content-Type": "text/html"});
response.write("<html><body><h1>Internal Server Error.</h1></body></html>");
response.end();
}
else
{
response.writeHead(200, {"Content-Type":type});
response.write(file);
response.end();
// parseObject("./objs/square pillar.obj");
}
});
}
//Serve the requested resource
function serveResource(response, request)
{
console.log("serving the requested resource");
var resource = url.parse(request.url).pathname;
var type = "text/";
if(/\.js$/.test(resource))
type += "JavaScript";
else if(/\.css$/.test(resource))
type += "css";
else if(/\.png$/.test(resource))
type = "image/png";
serveFile(response, "." + resource, type);
}
function serveJson(response, request)
{
var resource ="./jsons" + url.parse(request.url).pathname;
console.log("serving " + resource + " as json.");
serveFile(response, resource, "application/json");
}
//serve the start.html page
function start(response, request)
{
console.log("home page requested.");
serveFile(response, "./page.html", "text/html");
}
exports.serveResource = serveResource;
exports.start = start;
exports.serveJson = serveJson;