-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandler.js
108 lines (86 loc) · 2.71 KB
/
httphandler.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
101
102
103
104
105
106
107
108
requests = [];
const requestTypes = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE'
};
function createParsedRequest(type) {
let parsedRequest = {};
parsedRequest.id = requests.length;
parsedRequest = setRequestType(parsedRequest, type);
return parsedRequest;
}
function setRequestType(req, type) {
switch(type) {
case 'GET': req.type = requestTypes.GET; break;
case 'POST': req.type = requestTypes.POST; break;
case 'PUT': req.type = requestTypes.PUT; break;
case 'DELETE': req.type = requestTypes.DELETE; break;
}
return req;
}
function setHeaders(req, parsedRequest) {
parsedRequest.headers = req.headers;
return parsedRequest;
}
function setBody(req, parsedRequest) {
parsedRequest.body = req.body;
return parsedRequest;
}
function setRequestedURL(req, parsedRequest) {
parsedRequest.requestedUrl = req.originalUrl;
return parsedRequest;
}
function setTime(parsedRequest) {
parsedRequest.time = Date.now();
return parsedRequest;
}
function setCommonValues(req, parsedRequest) {
parsedRequest = setHeaders(req, parsedRequest);
parsedRequest = setBody(req, parsedRequest);
parsedRequest = setRequestedURL(req, parsedRequest);
parsedRequest = setTime(parsedRequest);
return parsedRequest;
}
function handleGET(req, res) {
let parsedRequest = createParsedRequest('GET');
parsedRequest = setCommonValues(req, parsedRequest);
displayRequest(parsedRequest, res);
}
function handlePOST(req, res) {
console.log(req);
let parsedRequest = createParsedRequest('POST');
parsedRequest = setCommonValues(req, parsedRequest);
displayRequest(parsedRequest, res);
}
function handlePUT(req, res) {
let parsedRequest = createParsedRequest('PUT');
parsedRequest = setCommonValues(req, parsedRequest);
displayRequest(parsedRequest, res);
}
function handleDELETE(req, res) {
let parsedRequest = createParsedRequest('DELETE');
parsedRequest = setCommonValues(req, parsedRequest);
displayRequest(parsedRequest, res);
}
function displayRequest(parsedReq, res) {
requests.unshift(parsedReq);
console.log(requests.length);
res.type('json');
res.end(JSON.stringify(parsedReq));
}
function displayRecordedRequests(res) {
res.render('view', {requests: requests});
}
function displayRecordedRequestsJSON(res) {
res.type('json');
res.end(JSON.stringify(requests));
}
exports.handleGET = handleGET;
exports.handlePOST = handlePOST;
exports.handlePUT = handlePUT;
exports.handleDELETE = handleDELETE;
exports.displayRequest = displayRequest;
exports.displayRecordedRequests = displayRecordedRequests;
exports.displayRecordedRequestsJSON = displayRecordedRequestsJSON;