-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiServer.js
More file actions
52 lines (44 loc) · 1.28 KB
/
ApiServer.js
File metadata and controls
52 lines (44 loc) · 1.28 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
const http = require('http');
const url = require('url');
const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
class ApiServer {
constructor(port){
this.port = port;
this.server;
this.router = [];
}
get(path, handler) {
this.router.push({
path : path,
method : 'GET',
handler : handler
});
}
listen() {
this.server = http.createServer((req, res) => {
this.handler(req, res);
}).listen(this.port);
console.log('API Server running at localhost:', this.port);
}
handler(req, res) {
var pathname = url.parse(req.url, true).pathname;
for(var i=0; i<this.router.length; i++){
var rule = this.router[i];
var matched = match(rule.path, { decode: decodeURIComponent })(pathname);
if(matched && (req.method === rule.method || rule.method === '*')) {
console.log('matched', pathname)
return rule.handler(req, res, matched);
}
}
return this.notSupported(req, res);
}
notSupported(req, res) {
res.writeHead(200, { 'Content-Type' : 'application/json; charset=UTF-8'});
console.warn(req.url);
res.end(JSON.stringify({
error: 'Method not supported.',
request: url.parse(req.url, true)
}));
}
}
module.exports = ApiServer;