-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (38 loc) · 1.18 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
43
44
45
46
47
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const swaggerUi = require("swagger-ui-express");
const constants = require("./utils/constants");
const handleError = require("./middlewares/handleError");
const routes = require("./routes");
const swaggerDocument = require("./swagger.json");
// Create Express server
const app = express();
// configure app to use body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Add HTTP request logger
app.use(morgan("combined"));
// Connect to MongoDB database
mongoose
.connect(`${constants.dbURI}/${constants.dbName}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
console.log("Connected to database");
})
.catch((err) => {
console.log(
"MongoDB connection error. Please make sure MongoDB is running. " + err
);
});
// Handle Auth Routes
app.use(routes);
// Handle Swagger UI
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(handleError);
app.set("port", process.env.PORT || 5200);
module.exports = app;