-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (34 loc) · 1.02 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
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const mongoose = require("mongoose");
const { errorHandler, routeNotFound } = require("./middlewares");
const routes = require("./routes");
const PORT = process.env.PORT || 5000;
const DB_URL = process.env.DB_URL;
const dbName = process.env.DB_NAME;
const app = express();
// Middleware at Application level
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/", routes);
app.get("/", (req, res) => {
res.send("Welcome to Knight-Studio Backend");
});
app.use(routeNotFound);
app.use(errorHandler);
mongoose.set("strictQuery", false);
mongoose
.connect(DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName,
})
.then(() => {
app.listen(PORT, () => {
console.log(`Server Started , Listening on port ${process.env.PORT}`);
console.log("Connected to database successfully!");
});
})
.catch((err) => console.log("Error in connecting to DB", err));