-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (34 loc) · 1.15 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
//Creating an Instance of the Application;
const express = require("express");
const connectDB = require("./db/connect.js");
const productsRoute = require("../starter/routes/products.js");
const notFoundMiddleware = require("./middleware/not-found.js");
const errorMiddleware = require("./middleware/error-handler.js");
require("dotenv").config();
// Creating an Instance of the Application
const app = express();
// Logging Middleware for Debugging
// Route: Testing Routes
app.get("/", (req, res) => {
res.send('<h1>04-Store-Api</h1><a href="/api/v1/products">products route</a>');
});
// Mount Products Routes
app.use("/api/v1/products", productsRoute);
// Middleware for 404 and Error Handling
app.use(notFoundMiddleware);
app.use(errorMiddleware);
// Declaring the port
const port = process.env.PORT || 2323;
// Starting the Application...
const startApplication = async () => {
try {
await connectDB(process.env.MONGO_URI);
app.listen(port, () => {
console.log(`Database connected. Server running on port: ${port}`);
});
} catch (error) {
console.error(`Error Spinning the Server: ${error}`);
}
};
// Invoking it ():
startApplication();