-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.59 KB
/
index.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
//dependencies
const http = require("http")
const stringDecoder = require("string_decoder").StringDecoder;
const url = require("url")
const handler = require("./lib/handlers")
const helpers = require("./lib/helpers")
removeSlashes = (ch) =>{
return ch.replace(/\//g,"");
}
//init the server
const server = http.createServer((req,res)=>{
var Url = req.url;
var parsedUrl = new URL(Url,"http://example.com/");
var pathName = removeSlashes(parsedUrl.pathname);
var headers = req.headers;
var queries = url.parse(req.url,true).query;
var method = req.method.toUpperCase();
buffer = '';
var decoder = new stringDecoder('utf-8')
req.on('data',(data)=>{
buffer += data;
})
req.on("end",()=>{
buffer += decoder.end();
var chosenHander = typeof(router[pathName])!='undefined' ? router[pathName] : handler.error;
data = {
"parsedUrl" : pathName,
"headers" : headers,
"queries" : queries,
"method" : method,
"payload" : helpers.parseJsonToObject(buffer),
};
chosenHander(data,(statusCode,payload)=>{
var statusCode = typeof(statusCode)=='number' ? statusCode : 200;
var payload = typeof(payload)=='object' ? payload : {};
var stringObject = JSON.stringify(payload)
res.setHeader('Content-Type','application/json');
res.writeHead(statusCode);
res.end(stringObject);
});
});
});
var router = {
"ping" : handler.test,
"users" : handler.users,
"tokens" : handler.tokens,
"checks" : handler.checks
};
server.listen(3000,()=>{
console.log("Listenning on port 3000")
});