-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (83 loc) · 2.92 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const http = require('http');
const https = require('https');
const url = require('url');
const StringDecoder = require('string_decoder').StringDecoder
const config = require('./config');
const fs = require('fs');
const handlers = require('./lib/handlers');
const httpServer = http.createServer((req,res) => {
unifiedServer(req,res);
});
httpServer.listen(config.httpPort, () => {
console.log( config.envName.toUpperCase() + " ENVIRONMENT");
console.log('Server is listening on port ' + config.httpPort);
});
const httpsServerOptions = {
'key': fs.readFileSync('./https/key.pem'),
'cert': fs.readFileSync('./https/cert.pem')
};
const httpsServer = https.createServer(httpsServerOptions,(req,res) => {
unifiedServer(req,res);
});
httpsServer.listen(config.httpsPort, () => {
console.log( config.envName.toUpperCase() + " ENVIRONMENT");
console.log('Server is listening on port ' + config.httpsPort);
});
// All the server logic for both http and https server
const unifiedServer = function(req,res) {
// Get the URL and parse it
const parsedUrl = url.parse(req.url, true);
// Get the path
const path = parsedUrl.pathname;
const trimmedPath = path.replace(/^\/+|\/+$/g,'');
// Get query string parameters as an object
const queryStringObject = parsedUrl.query;
// HTTP Method type
const method = req.method.toLowerCase();
// Get the headers as an object
const headers = req.headers;
// Get payloads
const decoder = new StringDecoder('utf-8');
let buffer = '';
// data event only gets called if payloads exist (body)
req.on('data', data => {
buffer += decoder.write(data);
});
req.on('end', () => {
buffer += decoder.end();
// Chose the handler for this request
let chosenHandler;
if(router[trimmedPath] == undefined) {
chosenHandler = handlers.notFound;
}else {
chosenHandler = router[trimmedPath];
}
// Construct data object
const data = {
'trimmedPath': trimmedPath,
'queryStringObject': queryStringObject,
'headers': headers,
'method': method,
'payload': buffer
}
// Router request to handler
chosenHandler(data, (statusCode, payload) => {
// Defualt status code
statusCode = typeof(statusCode === 'number') ? statusCode : 200;
// Default payload
payload = typeof(payload === 'object') ? payload : {};
//Convert payload to string
const payloadString = JSON.stringify(payload);
// Return the response
res.setHeader('Content-Type','application/json');
res.writeHead(statusCode);
res.end(payloadString);
console.log(statusCode, payloadString);
});
});
}
// Request Router
const router = {
ping: handlers.ping,
users: handlers.users
}