forked from billwert/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.js
More file actions
75 lines (61 loc) · 1.63 KB
/
Copy pathhelper.js
File metadata and controls
75 lines (61 loc) · 1.63 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
const Handlebars = require('handlebars');
const GREETING = "Hello, World!";
const self = module.exports = {
additionalFortune: () => ({
id: 0,
message: 'Additional fortune added at request time.'
}),
fortunesTemplate: Handlebars.compile([
"<!DOCTYPE html>",
"<html>",
"<head><title>Fortunes</title></head>",
"<body>",
"<table>",
"<tr>",
"<th>id</th>",
"<th>message</th>",
"</tr>",
"{{#fortunes}}",
"<tr>",
"<td>{{id}}</td>",
"<td>{{message}}</td>",
"</tr>",
"{{/fortunes}}",
"</table>",
"</body>",
"</html>"
].join('')),
randomTfbNumber: () => Math.floor(Math.random() * 10000) + 1,
fillArray: (value, len) => {
const arr = [];
for (let i = 0; i < len; i++) {
arr.push(value);
}
return arr;
},
addTfbHeaders: (res, headerType) => {
const headerTypes = {
plain: 'text/plain',
json: 'application/json',
html: 'text/html; charset=UTF-8'
};
res.setHeader('Server', 'Node');
res.setHeader('Content-Type', headerTypes[headerType]);
},
responses: {
jsonSerialization: (req, res) => {
const HELLO_OBJ = { message: GREETING };
self.addTfbHeaders(res, 'json');
res.end(JSON.stringify(HELLO_OBJ));
},
plaintext: (req, res) => {
self.addTfbHeaders(res, 'plain');
res.end(GREETING);
},
routeNotImplemented: (req, res) => {
res.writeHead(501, {'Content-Type': 'text/plain; charset=UTF-8'});
const reason = { reason: "`" + req.url + "` is not an implemented route" };
res.end(JSON.stringify(reason));
}
}
};