-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (36 loc) · 988 Bytes
/
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
const mongoose = require("mongoose");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
require("dotenv").config();
const userRoutes = require("./routes/user");
const taskRoutes = require("./routes/task");
// Initialising variables
const app = express();
const dbUrl = process.env.DB;
// Mongoose connection
mongoose
.connect(dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
console.log("DB is successfully connected");
})
.catch((err) => {
console.log(`Not able to connect to DB, ${err}`);
});
// Middlewares
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
// Routes
app.use("/api", userRoutes);
app.use("/api", taskRoutes);
// Port
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server successfully running on port ${port}`);
});