-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (55 loc) · 1.89 KB
/
app.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
/*Title: Task Manager App - app.js
Description: Build a RESTful API for a Task Manager App. Using Node, Express and MongoDB.
Author: Md. Samiur Rahman (Mukul)
Website: http://www.SamiurRahmanMukul.epizy.com
Github: https://www.github.com/SamiurRahmanMukul
Email: [email protected] [FAKE EMAIL]
Date: 08/11/2021 */
// !TASK MANAGER APP - ROUTES
// app.get("/api/v1/tasks"); - GET ALL THE TASKS
// app.get("/api/v1/tasks/:id"); - GET SINGLE TASK
// app.post("/api/v1/tasks"); - CREATE A NEW TASK
// app.patch("/api/v1/tasks/:id"); - UPDATE A TASK
// app.delete("/api/v1/tasks/:id"); - DELETE A TASK
// external modules import
const express = require("express");
const dotenv = require("dotenv");
const favicon = require("serve-favicon");
// internal modules import
const tasksRoute = require("./src/routes/tasksRoute");
const connectDB = require("./src/db/connect");
const notFoundHandler = require("./src/middleware/not-found");
// application configuration
const app = express();
dotenv.config();
const PORT = process.env.PORT || 5000;
const MONGO_URI = process.env.MONGO_URI;
// establish connection to mongodb
// require("./src/db/connect");
const dbEstablished = async () => {
try {
await connectDB(MONGO_URI);
console.log("Connected to MongoDB successfully !!!");
} catch (err) {
console.log("Error connecting to MongoDB: ", err.message);
process.exit(1);
}
};
dbEstablished();
// static folder & favicon setup
app.use(express.static(__dirname + "/public"));
app.use(favicon(__dirname + "/public/favicon.ico"));
// application middleware
app.use(express.json());
// application routes
app.use("/api/v1/tasks", tasksRoute);
// not found handler
app.use(notFoundHandler);
// application listening
app.listen(PORT, (err) => {
if (err) {
console.log("Listening error: " + err);
} else {
console.log(`Server is running on http://localhost:${PORT}`);
}
});