-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-.js
More file actions
125 lines (96 loc) · 3.29 KB
/
test-.js
File metadata and controls
125 lines (96 loc) · 3.29 KB
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
/*
*
* Premier fichier pour API
*
*/
//Dependencies
const config = require('./config');
const http = require('http');
const https = require('https');
const url = require('url');
const StringDecoder = require('string_decoder').StringDecoder;
const fs = require('fs');
//The serer should respond to all requests with a string
//Instantiate http server
let httpServer = http.createServer((req,res) => unifiedServer(req,res) );
//Instantiate https server
let httpsServerOptions = {
'key': fs.readFileSync('./https/key.pem'),
'cert': fs.readFileSync('./https/cert.pem')
};
let httpsServer = https.createServer(httpsServerOptions , (req,res) => unifiedServer(req,res) );
//start the http httpServer
httpServer.listen(config.httpPort, () => {
console.log(`httpServer is listening on port ${config.httpPort} in ${config.envName} mode`);
});
//start the https httpServer
httpsServer.listen(config.httpsPort, () => {
console.log(`httpsServer is listening on port ${config.httpsPort} in ${config.envName} mode`);
});
// All the serer logic for both http and https httpServers
let unifiedServer = (req,res) => {
//Get the url and parseit
let parsedUrl = url.parse(req.url,true);
//Get the path from the url
let path = parsedUrl.pathname;
let trimmedPath = path.replace(/^\/+|\/+$/g, '');
//Get the querystring as an object
let queryStringObject = parsedUrl.query;
//Get the http methode
let method = req.method.toLowerCase();
//Get the headers as an object
let headers = req.headers;
console.log(`headers: ,${headers}`);
console.log(`querystring: ,${JSON.stringify(queryStringObject)}`);
//Get the payload if
let decoder = new StringDecoder('utf-8');
let buffer = '';
req.on('data', (data) => {
buffer += decoder.write(data);
});
req.on('end', () => {
buffer += decoder.end
//Choose de handler thi request should go to
let chosenHandlers = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound;
//Construct data object to snd to the handlers
let data = {
'trimmedPath': trimmedPath,
'queryStringObject': queryStringObject,
'method': method,
'headers': headers,
'payload': buffer
}
//Route the request to the handler specify in th router
chosenHandlers(data, (statusCode, payload) => {
//use statuscode called back by the handler or default to 202
statusCode = typeof(statusCode) === 'number' ? statusCode : 200;
//use payload called back by the handler or default to an empty object
payload = typeof(payload) === 'object' ? payload : {};
//convert the payload to a string_decoder
let payloadString = JSON.stringify(payload);
//Return the response
res.setHeader('Content-Type','application/json')
res.writeHead(statusCode);
res.end(payloadString);
//Logs
console.log(`Returning this response: ${statusCode} , ${payload}`);
});
});
};
//Define handlers
let handlers = {};
handlers.sample = (data,callback) => {
//callback http status code and a payload
callback(406,{'name' : 'sample handlers'});
};
handlers.ping = (data,callback) => {
callback(200);
};
handlers.notFound = (data,callback) => {
callback(404);
};
//Define a request router
let router = {
'sample' : handlers.sample,
'ping' : handlers.ping
};