-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.51 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import app from "./server.js"
import dotenv from "dotenv"
import mongodb from 'mongodb'
import { MongoMemoryServer } from 'mongodb-memory-server'
let MongoClient
if (process.env.NODE_ENV === 'test') {
const mongodb = require('mongodb')
const { MongoMemoryServer } = require('mongodb-memory-server')
MongoClient = mongodb.MongoClient
} else {
MongoClient = mongodb.MongoClient
}
// DAO
import UsersDAO from "./dao/UsersDAO.js"
import PostsDAO from "./dao/PostsDAO.js"
import UsersDAOV2 from "./dao/v2/UsersDAO.js"
import PostsDAOV2 from "./dao/v2/PostsDAO.js"
import UpdateMembersDAO from "./dao/v2/UpdateMembersDAO.js"
dotenv.config()
const port = process.env.PORT || 8080
let mongod;
;(async () => {
try {
let dbUrl = process.env.ATLAS_URI
if (process.env.NODE_ENV === 'test') {
mongod = await MongoMemoryServer.create();
dbUrl = mongod.getUri();
}
const client = await MongoClient.connect(dbUrl, { useNewUrlParser: true })
await UsersDAO.injectDB(client)
await PostsDAO.injectDB(client)
await UsersDAOV2.injectDB(client)
await PostsDAOV2.injectDB(client)
await UpdateMembersDAO.injectDB(client)
if (process.env.NODE_ENV !== "test") {
app.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
}
}
catch (err) {
console.error(err.stack)
process.exit(1)
}
})()
if (process.env.NODE_ENV === 'test') {
module.exports = app
}