-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (77 loc) · 2.33 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
/***
*
* Node.js Master Class
* Assignment # 1
* Author: Frank Ruiz
*
*/
var http = require("http");
var url = require("url");
var StringDecoder = require("string_decoder").StringDecoder;
var server = http.createServer((req, res) => {
// Get the URL en parse it
var parsedUrl = url.parse(req.url, true);
// Get the current path
var path = parsedUrl.pathname;
// Trim de path
var trimmedPath = path.replace(/^\/+|\/+$/g, "");
// Get the query string a an object
var queryString = parsedUrl.query;
// Get the HTTP method
var method = req.method.toLowerCase();
// Get the headers as an object
var headers = req.headers;
// Get the payload
var decoder = new StringDecoder("utf-8");
var buffer = "";
req.on("data", data => (buffer += decoder.write(data)));
req.on("end", () => {
buffer += decoder.end();
var chosenHandler =
typeof router[trimmedPath] !== "undefined"
? router[trimmedPath]
: handlers.notFound;
// Construct the data object to send to the handler
var data = {
trimmedPath: trimmedPath,
queryString: queryString,
method: method,
headers: headers,
payload: buffer
};
chosenHandler(data, (statusCode, payload) => {
// Use the status code called back by the handler, or default
statusCode = typeof statusCode == "number" ? statusCode : 200;
// Use the payload called back by the handler, or default to
payload = typeof payload == "object" ? payload : {};
// Convert the payload to JSON format
var payloadString = JSON.stringify(payload);
// Set JSON response
res.setHeader("Content-Type", "application/json");
// Set the head status
res.writeHead(statusCode);
// Send the response
res.end(payloadString);
// Log the request path
console.log("Returning this response: ", statusCode, payloadString);
});
});
});
// Start the server
server.listen(3000, () => {
console.log("Listening on http://localhost:3000");
});
// Define the handlers
var handlers = {};
// greeting handler
handlers.greeting = (data, callback) => {
callback(406, { greeting: "Hello evarybody I'm an Node.js API response!" });
};
// not found handler
handlers.notFound = (data, callback) => {
callback(404);
};
// Define routers
var router = {
hello: handlers.greeting
};