-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (43 loc) · 1.74 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
//imported express library.
const express = require("express");
//app is to run the application by calling express function.
const app = express();
//To connect to our Server
const mongoose = require("mongoose");
//importing the dotenv file for fetching the secure url for connection to database
const dotenv = require("dotenv");
//instead of having all end points of here in the page,
// we are using a separate file for each route to user , productetc.
const userRoute = require("./routes/user");
const authRoute = require("./routes/auth");
const productRoute = require("./routes/product");
const cartRoute = require("./routes/cart");
const orderRoute = require("./routes/order");
dotenv.config();
//To connect to our MongoDB Database
mongoose
.connect(process.env.MONGO_URL)
.then(() => console.log("DB CONNNECTION SUCCESSFUL!!! :) ")) //if connection successful then print this statement
.catch((err) => {
//else print this statement
console.log(err); //prints the error
});
//=========================================================
//ENDPOINTS:
app.get("/api/test", () => {
console.log("Test is SUCCESSFUL!");
});
//To parse json objects as input
app.use(express.json());
//This means that whenever we go to the API end point /api/user , the application will use userRoute.
app.use("/api/users", userRoute);
app.use("/api/auth", authRoute);
app.use("/api/products", productRoute);
app.use("/api/carts", cartRoute);
app.use("/api/orders", orderRoute);
//=============================================================
//Port to see and listen to our runnning application
//If no port number in env file then start it at Port number : 5000
app.listen(process.env.PORT || 5000, () => {
console.log("Backend server is running!");
});