-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.js
More file actions
81 lines (72 loc) · 2.24 KB
/
endpoint.js
File metadata and controls
81 lines (72 loc) · 2.24 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
'use strict';
const chalk = require('chalk');
class Endpoint {
constructor (method, rgx, callback) {
this.method = method.toUpperCase();
this.callback = callback;
if (typeof rgx === 'string') { // presumes a path
if (~rgx.indexOf(":")) { // includes an optional parameter
this.parameters = rgx.slice(1).split("/");
this.parameters.forEach(function(cur, i, arr){
if (cur.indexOf(":") === 0) {
arr[i] = cur.slice(1);
} else {
arr[i] = "";
}
});
this.rgx = new RegExp("^" + rgx.replace(/\/\:\w+/g, '\/([^\/]*)'));
// "^" to check if the string is at the start of the url
// replacer allows the regex to part match any string where an optional parameter has been specified with a ":"
} else {
this.rgx = new RegExp("^" + rgx);
this.parameters = [];
}
} else if (rgx instanceof RegExp){ // any other regex match
this.rgx = rgx;
this.parameters = [];
} else {
throw TypeError("Endpoints must be either a RegExp or String");
}
}
match (request) {
return (this.method === request.method || this.method === "ALL") && this.rgx.test(request.path);
}
run (request, res) {
this.getParameters(request);
try {
this.callback(request, res);
} catch (e) {
Endpoint.generateError(res, 500, "Failed to run command, syntax error in callback", e.stack || e);
}
}
getParameters (request) {
const parts = request.path.slice(1).split("/");
const l = this.parameters.length;
let i = 0;
let parameterName;
for (; i < l; i++) {
parameterName = this.parameters[i];
if (parameterName) {
request.parameters[parameterName] = parts[i];
}
}
}
static generateLog (res, req) {
const DATE = new Date().toLocaleString('en-GB');
const TIME = process.hrtime(request.time);
const CODE = res.statusCode;
const COLOR = chalk[
CODE > 500 ? 'bgRed' :
CODE > 400 ? 'bgYellow' :
CODE > 200 ? 'bgGreen' :
'dim' // for whatever reason the request has completed with an odd status code
];
console.log(`${DATE} | ${COLOR(CODE)} | ${TIME[0] + TIME[1] / 1e9} | ${req.method} ${req.path}`);
}
static generateError (res, code, statement, request) {
res.statusCode = code;
res.end(code + " " + statement);
request.error = statement;
}
}
module.exports = Endpoint;