-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
76 lines (55 loc) · 2.42 KB
/
index.ts
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
import * as http from 'http';
import * as url from 'url';
import * as string_decoder from 'string_decoder';
import { handlers } from './lib/handlers';
import { helpers } from './lib/helpers';
import { env } from './ecosystem.config';
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const path = parsedUrl.pathname;
const trimmedPath = path.replace(/^\/+|\/+$/g, '');
const httpMethod = req.method;
const queryStringParamsObject = parsedUrl.query;
const headers = req.headers;
const decoder = new string_decoder.StringDecoder('utf-8');
let buffer = '';
req.on('data', (data: any) => {
buffer += decoder.write(data);
});
req.on('end', () => {
buffer += decoder.end();
const chosenHandler = routes[trimmedPath] ? routes[trimmedPath] : handlers.notFound;
const data = {
'trimmedPath': trimmedPath,
'queryStringParamsObject': queryStringParamsObject,
'method': httpMethod,
'headers': headers,
'payload': helpers.parseJsonToObject(buffer)
};
chosenHandler(data, (statusCode: number, payload: object, contentType: string) => {
contentType = contentType ? contentType : 'json';
statusCode = statusCode && typeof statusCode === 'number' ? statusCode : 406;
let payloadString = '';
if (contentType === 'json') {
res.setHeader('Content-Type', 'application/json');
payload = payload && typeof payload === 'object' ? payload : {};
payloadString = JSON.stringify(payload);
} else if (contentType === 'html') {
res.setHeader('Content-Type', 'text/html');
payloadString = payload && typeof (payload) === 'string' ? payload : '';
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.writeHead(statusCode);
res.end(payloadString);
});
});
});
server.listen(env.port, () => {
console.log('\x1b[32m%s\x1b[0m', `Binance Trader Bot Started at Port ${env.port} in ${env.envName} mode!`);
});
const routes = {
'': handlers.index,
'ping': handlers.ping
};