-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
215 lines (182 loc) · 5.6 KB
/
app.js
File metadata and controls
215 lines (182 loc) · 5.6 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
var http = require("http"),
fs = require('fs'),
path = require("path"),
mime = require("mime"),
root = __dirname;
// include external apis
var auth = require('./auth');
function errorFactory(){
// Error Code
this.BAD_REQUEST = 400;
this.NOT_FOUND = 404;
this.UNAUTHORIZED = 403;
this.INTERNAL_SERVER_ERROR = 500;
this.METHOD_NOT_ALLOWED = 405;
// instance member
this.badRequestError = function(){
return "Bad Request Error";
};
this.fileNotFoundError = function(){
return "Not Found";
};
this.unauthorized = function(){
return "Unauthorized";
};
this.internalServerError = function(){
return "Internal Server Error";
};
this.methodNotAllowed = function(){
return "Method Not Allowed";
};
}
function app(){ };
app.prototype.errors = new errorFactory();
app.prototype.routes = [];
app.prototype.config = {
controllerPath: path.join(root + "/controllers"),
viewPath: path.join(root + "/views"),
viewExtension: ".html" // default
};
app.prototype.initLoad = function(){
console.log("InitLoad InProgress...");
var controllerPath = this.config.controllerPath;
if(fs.existsSync(controllerPath)){
fs.readdir(controllerPath, function(error, files){
files.forEach(function(file, index, files){
require(path.join(controllerPath + '/' + file));
});
});
console.log("InitLoad Done...");
}else{
response.statusCode = this.errors.METHOD_NOT_ALLOWED;
response.end(that.errors.fileNotFoundError());
}
};
app.prototype.setPath = function(path, httpMethod, callback, attribute){
///<summary> Setting path and its corresponding action callback </summary>
this.routes.forEach(function(route, index, routes){
if(route["path"].toLowerCase() === path.toLowerCase()){
throw new Error("Duplicate routes present. Cannot procees further...");
}
});
this.routes.push({
path : path, // url (relative)
httpMethod : httpMethod, // GET or POST
callback : callback, // callback function
attribute : !!attribute ? attribute: {auth: false}
});
//console.log(this.routes);
};
app.prototype.render = function(filePath){
var mimeType = mime.lookup(filePath),
viewPath = this.config.viewPath,
requestedFilePath = path.join(viewPath, filePath),
stream,
request = this.request,
response = this.response,
that = this;
console.log(requestedFilePath + request.method);
switch(request.method){
case 'GET':
fs.stat(requestedFilePath, function(error, stats){
if(error){
if(error.code === 'ENOENT'){
response.statusCode = that.errors.NOT_FOUND;
response.end("Not Found");
}else{
response.statusCode = that.errors.INTERNAL_SERVER_ERROR;
response.end("Internal Server Error");
}
} else {
response.setHeader("Content-Length",stats.size);
response.setHeader("Content-Type", mimeType);
response.statusCode = 200;
// Read the file from the server location
stream = fs.createReadStream(requestedFilePath);
stream.pipe(response);
stream.on("error", function(error){
response.statusCode = that.errors.INTERNAL_SERVER_ERROR;
response.end("Internal Server Error");
});
}
});
break;
case 'POST':
var item = null;
request.setEncoding("utf8");
request.on("data", function(reqChunk){
item +=reqChunk;
console.log(item);
});
request.on("end", function(){
// response.end(item);
// Save this data in Indexed DB
fs.stat(requestedFilePath, function(error, stats){
if(error){
if(error.code === 'ENOENT'){
response.statusCode = that.errors.NOT_FOUND;
response.end("Not Found");
}else{
response.statusCode = that.errors.INTERNAL_SERVER_ERROR;
response.end("Internal Server Error");
}
} else {
response.setHeader("Content-Length",stats.size);
response.setHeader("Content-Type", mimeType);
response.statusCode = 200;
// Read the file from the server location
stream = fs.createReadStream(requestedFilePath);
stream.pipe(response);
stream.on("error", function(error){
response.statusCode = that.errors.INTERNAL_SERVER_ERROR;
response.end("Internal Server Error");
});
}
});
});
break;
}
};
app.prototype.startServer = function(port){
var that = this;
var url = require("url"); // Get the file server root directory path
http.createServer(function(request, response){
if(!!request){
//console.log(request.url);
var parsedUrl = url.parse(request.url),
routeFound = false;
console.log("Url : "+parsedUrl.pathname);
that.routes.forEach(function(route, index, array){
if(parsedUrl.pathname.toLowerCase().indexOf(route.path.toLowerCase()) !== -1
&& route.httpMethod.toLowerCase() === request.method.toLowerCase()){
// Extract req header body payload.
that.request = request;
that.response = response;
//console.log(path.extname(parsedUrl.pathname));
console.log("auth: "+route.attribute.auth);
// check for authorization/ authentication
if(route.attribute.auth && !auth.onAuthorization.call(that)){
response.statusCode = that.errors.UNAUTHORIZED;
response.end(that.errors.unauthorized());
return;
}
if(path.extname(parsedUrl.pathname) === ""){
route.callback.call(that, request, response);
}else{
// render assocaited files here.
that.render(parsedUrl.pathname.toLowerCase());
}
routeFound = true;
return false;
}
});
if(!routeFound){
// Route does not exist.
response.end(that.errors.fileNotFoundError());
}
}
}).listen(port);
console.log("Server started...");
};
module.exports = new app();