-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (68 loc) · 2.32 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
/*
* Primary file for the API
*
*/
// Dependencies
const config = require('./config/config');
let http = require('http');
let url = require('url');
const StringDecoder = require('string_decoder').StringDecoder;
let handlers = require('./lib/handlers');
// Instantiate the HTTP server
let httpServer = http.createServer(function(req, res) {
unifiedServer(req, res);
});
// Start the server
httpServer.listen(config.httpPort,function() {
console.log('The server is listning on port '+config.httpPort+' in '+config.envName+' mode!\n');
});
// All the server logic for both the http and https server
var unifiedServer = function (req,res) {
// Get the url and parse it
let parsedUrl = url.parse(req.url,true);
// Get the path from url
let path = parsedUrl.pathname;
let trimmedPath = path.replace(/^\/+|\/+$/g,'');
// Get the headers as an object
let headers = req.headers;
// Get the http method
let method = req.method.toLowerCase();
// Get the payload, if there is any
let decoder = new StringDecoder('utf-8');
let buffer = '';
// on request event 'data'
req.on('data', function(data){
buffer += decoder.write(data);
});
req.on('end', function(){
buffer += decoder.end();
// Chose the handler this request goes to. If one is not found use notFound
let chosenHandler = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound;
// Construct the data object to send to the handler
let data = {
'trimmedPath': trimmedPath,
'method': method,
'headers': headers,
'payload': buffer
};
// Route the request to the handler specified in the router
chosenHandler(data, function(statusCode, payload) {
// Use the status code called back by the handler, or default to 200
statusCode = typeof(statusCode) == 'number' ? statusCode : 200;
// Use the payload called back by the handler, or default to en empty object
payload = typeof(payload) == 'object' ? payload : {};
// Convert the object to a string
let payloadString = JSON.stringify(payload);
// Return the response
res.setHeader('Content-Type', 'application/json');
res.writeHead(statusCode);
res.end(payloadString);
// Log the request path
console.log('Returning this response: ',statusCode,payloadString);
});
});
};
// request route / handler
let router = {
'hello': handlers.hello
};