forked from apiacademy/ndcoslo2015
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
100 lines (88 loc) · 2.07 KB
/
utils.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************
* todo-mvc implementation based on ALPS doc
* utilities (server)
* May 2015
* Mike Amundsen (@mamund)
* Soundtrack : Complete Collection : B.B. Kind (2008)
*******************************************************/
/*
var fs = require('fs');
var folder = process.cwd() + '/files/';
var httpActions = {};
httpActions.append = "POST";
httpActions.partial = "PATCH";
httpActions.read = "GET";
httpActions.remove = "DELETE";
httpActions.replace = "PUT";
exports.actionMethod = function(action, protocol) {
var p = protocol||"http";
var rtn = "GET";
switch(p) {
case "http":
rtn = httpActions[action];
break;
default:
rtn = "GET";
}
return rtn;
}
exports.errorResponse = function(req, res, msg, code, description) {
var doc;
doc = {};
doc.error = {};
doc.error.code = code;
doc.error.message = msg;
doc.error.url = 'http://' + req.headers.host + req.url;
if (description) {
doc.error.description = description;
}
return {
code: code,
doc: doc
};
}
exports.file = function(req, res, parts, respond) {
var body, doc, type;
try {
body = fs.readFileSync(folder + parts[1]);
type = 'text/plain';
if (parts[1].indexOf('.js') !== -1) {
type = 'application/javascript';
}
if (parts[1].indexOf('.css') !== -1) {
type = 'text/css';
}
if (parts[1].indexOf('.html') !== -1) {
type = 'text/html';
}
respond(req, res, {
code: 200,
doc: body,
headers: {
'content-type': type
},
file: true
});
} catch (ex) {
respond(req, res, errorResponse(req, res, "File Not Found", 404));
}
}
//TK: is this in use?
exports.exception = function(name, message, code) {
var rtn = {};
rtn.type = "error";
rtn.name = name;
if (message) {
rtn.message = message;
} else {
rtn.message = "Error";
}
if (code) {
rtn.code = code;
} else {
rtn.code = 400;
}
return rtn;
}
// EOF
*/