forked from juliomoura-ilia/desafio-ilia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.21 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
40
require("dotenv").config();
const { MongoMemoryServer } = require("mongodb-memory-server");
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const routes = require("./routes");
const app = express();
const port = process.env.PORT || 80;
const connectToMongoDB = async () => {
if (process.env.DATABASE_URI) {
return mongoose.connect(process.env.DATABASE_URI, { useNewUrlParser: true, useUnifiedTopology: true });
} else {
const mongoServer = new MongoMemoryServer();
await mongoServer.start();
const mongoUri = await mongoServer.getUri();
return mongoose.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true });
}
};
connectToMongoDB()
.then(() => {
console.log(`Connected to MongoDB in ${process.env.NODE_ENV} mode`);
if (require.main === module) {
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
}
})
.catch((err) => {
console.error("Error connecting to MongoDB:", err.message);
});
app.use(express.json());
app.use(bodyParser.json());
app.use("/", routes);
module.exports = app;