-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.js
75 lines (57 loc) · 1.76 KB
/
Controller.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
var Twitter = require("./Twitter.js").Twitter;
var fs = require('fs');
var url = require("url");
var Controller = {
index : function(req, res) {
serveFile(req, res, "index.html");
},
search : function(req, res) {
var dataCallback = function(data) {
res.write(data);
};
var endCallback = function() {
res.end();
};
var twitterApi = new Twitter(require("url").parse(req.url, true).query.term, dataCallback, endCallback);
twitterApi.searchTweets();
},
error : function(req, res) {
res.writeHead(404);
res.end("Page not found");
},
public: function(req, res) {
var reqUrl = url.parse(req.url, true);
reqUrl = reqUrl.pathname.substr(1);
var filePath = reqUrl.split('/');
serveFile(req, res, filePath[1]);
}
};
/**
* Figure out if the file exists in the files system (under /public folder)
* if it does, then choose the correct content-type (look at the file extension)
* if it doesn't , give back a 404 response
* @param req
* @param res
* @param filePath
*/
var serveFile = function(req, res, filePath) {
//TODO: check for errors (file doesn't exist)
var fileExtension = filePath.split('.')[filePath.split('.').length-1];
var contentType;
if (fileExtension == "js") {
contentType = "text/javascript";
} else if (fileExtension == "html") {
contentType = "text/html";
}
res.writeHead(200, {
'content-type': contentType
});
var readStream = fs.createReadStream("./public/"+filePath);
readStream.on('data', function(data) {
res.write(data);
});
readStream.on('end', function() {
res.end();
});
}
exports.Controller = Controller;