-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.49 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
const http = require("http");
const errors = require("./errors");
const methods_handler = require("./methods_handler");
let server;
let ContentType = 'text/plain'; // default ContentType;
let hppJson = () => ContentType = 'application/json';
let hppCustomContentType = (_custom = String(_custom)) => ContentType = _custom;
let get_arr = [];
let post_arr = [];
let createApplication = () => {
return {
use: (fn) => {
fn();
},
get: (path, callback) => {
get_arr.push([path, callback]);
},
post: (path, callback) => {
post_arr.push([path, callback]);
},
listen: (_port, fn) => {
if (isNaN(Number(_port))) throw (errors.NotPortValidFormat(_port));
_port = Number(_port);
let return_value =
http.createServer((req, res) => {
() => [res.writeHead(200, { 'Content-Type': ContentType }), res.setHeader(200, { 'Content-Type': ContentType })];
switch (req.method) {
case 'GET':
return methods_handler['GET'](req, res, get_arr);
break;
case 'POST':
return methods_handler['POST'](req, res, post_arr);
break;
}
});
fn();
server = return_value;
return_value.listen(_port);
}
}
}
module.exports = {
hppJson,
createApplication
}