-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
64 lines (50 loc) · 1.72 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
// Env variables from config.js
const { port } = require("./src/config/config");
// Express lib
const express = require("express");
// Body parser lib
const bodyParser = require("body-parser");
// CORS lib
const cors = require("cors");
// Morgan lib logger middleware https://www.npmjs.com/package/morgan
// To log app activities a good way to keep track of what is going on
// Useful also to the debug issues when an exception come ups
const morgan = require("morgan");
// Defining the server/app
const server = express();
// List routes
const listEndpoints = require("express-list-endpoints");
// Using CORS
server.use(cors());
// We use Morgan to log our server
// "tiny" The minimal output of the log the default light param
server.use(morgan("tiny"));
// Body parse JSON
// Returns middleware that only parses json
server.use(bodyParser.json());
// Express urlencoded
// Returns middleware that only parses urlencoded with the QueryString module
/**
* You NEED express.json() and express.urlencoded()
* for POST and PUT requests,
* because in both these requests you are sending data (in the form of some data object)
* to the server and you are asking the server to accept or store that data (object),
* which is enclosed in the body (i.e. req.body) of that (POST or PUT) Request
*
*/
server.use(
bodyParser.urlencoded({
extended: true
})
);
// Using the Body parser lib
server.use(bodyParser.json());
// Main Routing
server.use(require("./src/routes/index.routes"));
// Endpoints list
console.log(listEndpoints(server));
// Starting the server on env port
server.listen(`${port}`, () => {
// Showing a message to the console informing on which port is running
console.log(`Server is running on port ${port}`);
});