-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (38 loc) · 1.24 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
const http = require("http");
const router = require("./routes/route.js");
const connectToDb = require("./database/db.js");
const port = 3000;
let vezes = 0;
connectToDb();
async function responseMiddleware(
responseObject,
{ statusCode, Location = "", ContentType = "", responseData }
) {
if (statusCode && Location) {
responseObject.writeHead(statusCode, { Location: Location });
responseObject.end();
} else {
responseObject.setHeader("Content-type", ContentType);
responseObject.end(responseData);
}
}
function startServer() {
const server = http.createServer(async (req, res) => {
vezes += 1;
console.log(`\nINFO - [${req.method} ${req.url}] Server Callback Function: ${vezes}° call received from browser`)
const { statusCode, Location, ContentType, responseData } = await router(req);
responseMiddleware(res, {
statusCode,
Location,
ContentType,
responseData,
});
});
server.on("error", (e) => {
console.error(`Erro no Server! ${e}`);
});
server.listen(port, () => {
console.log(`INFO - [SERVER] Server is running on port: http://localhost:${port} `);
});
}
startServer();