-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (98 loc) · 3.3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var http = require('http');
var https = require('https');
var url = require('url');
var StringDecoder = require('string_decoder').StringDecoder;
var config = require('./config');
var fs = require('fs');
// Create an HTTP server
var httpServer = http.createServer((req, res) => {
serverFunction(req, res)
});
// Create an HTTPS server
// Note, for the purpose of the application, I have generated an HTTPS cert.pem and key.pem
// files in order to make the HTTPS version work. However, in realtime production server,
// this 2 files should not be publicly revealed.
var httpsServerOptions = {
'key': fs.readFileSync('./https/key.pem'),
'cert': fs.readFileSync('./https/cert.pem')
};
var httpsServer = http.createServer(httpsServerOptions, (req, res) => {
serverFunction(req, res)
});
var serverFunction = (req, res) => {
// Get the parsed URL
var parsedUrl = url.parse(req.url, true);
// Get URL path
var path = parsedUrl.pathname;
// Get trimmed path name
var trimmedPath = path.replace(/^\/+|\/+$/gm,'');
console.log("Path: ",trimmedPath)
// Get request method
var method = req.method.toLowerCase()
// Get request headers
var headers = req.headers
// Get payload and parse it in as utf-8
decoder = new StringDecoder('utf-8')
var buffer = ''
// Get the query-strings as an object
var queryString = parsedUrl.query
// Parse the query string as on object
var queryStringObject = JSON.stringify(queryString)
// Listen to request data
req.on('data', (data) => {
buffer += decoder.write(data);
})
// Choose an handler for the request
var chosenHandler = typeof router[trimmedPath] !== 'undefined' ? router[trimmedPath] : handlers.notFound;
var data = {
'trimmedPath': trimmedPath,
'queryStringObject': queryStringObject,
'method': method,
'headers': headers,
'payload': buffer
};
//
var statusCode = 200
// Call the chosenHandler methode
chosenHandler(data, (statusCode, payload) => {
var payload = typeof (payload) == 'object' ? payload : {}
var payloadString = JSON.stringify(payload)
var statusCode = typeof (statusCode) == 'number' ? statusCode : 200;
var responseHeader = {
'Content-Type': 'application/json'
}
// Write the response head
res.writeHead(statusCode, responseHeader)
// Write the response body
res.end(payloadString)
})
// End the buffer when the request is ended
req.on('end', () => {
buffer += decoder.end()
})
}
// Set HTTP listen
httpServer.listen(config.httpPort, () => {
console.log(`Now listening to port ${config.httpPort} through on ${config.envName}.`)
});
// Set HTTPS listen
httpsServer.listen(config.httpsPort, () => {
console.log(`Now listening to port ${config.httpsPort} on ${config.envName} mode`)
});
// Create request handlers
var handlers = {
hello: (data = null, callback) => {
callback(200, {
'message': `Hi, how're you doing?`
})
},
notFound: (data = null, callback) => {
callback(404, {
'message': `Sorry, resource not found! maybe you mean a resource at /hello.`
})
}
}
// Create router
var router = {
hello: handlers.hello
};