-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (35 loc) · 968 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
44
45
const express = require("express");
const mongoose = require("mongoose");
const userHandler = require("./routeHandler/userHandler");
const dotenv = require("dotenv");
// express app initialization
const app = express();
dotenv.config();
app.use(express.json());
// application routes
app.use("/user", userHandler);
// database connection with mongoose
async function myDB() {
try {
await mongoose.connect(process.env.DB_CONNECTION_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("connection successful");
} catch (error) {
console.log(error)
}
}
// default error handler
const errorHandler = (err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
res.status(500).json({ error: err });
}
app.use(errorHandler);
// App Listen
app.listen(process.env.PORT || 3000, () => {
myDB();
console.log("app listening at port 3000");
});