-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
36 lines (32 loc) · 1.07 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
const express = require('express')
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient
const dbName = process.env.NODE_ENV === 'dev' ? 'database-test' : 'database'
const url = `mongodb://${process.env.MONGO_INITDB_ROOT_USERNAME}:${process.env.MONGO_INITDB_ROOT_PASSWORD}@${dbName}:27017?authMechanism=SCRAM-SHA-1&authSource=admin`
const options = {
useNewUrlParser: true,
reconnectTries: 60,
reconnectInterval: 1000
}
const routes = require('./routes/routes.js')
const port = process.env.PORT || 80
const app = express()
const http = require('http').Server(app)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/api', routes)
app.use((req, res) => {
res.status(404)
})
MongoClient.connect(url, options, (err, database) => {
if (err) {
console.log(`FATAL MONGODB CONNECTION ERROR: ${err}:${err.stack}`)
process.exit(1)
}
app.locals.db = database.db('api')
http.listen(port, () => {
console.log("Listening on port " + port)
app.emit('APP_STARTED')
})
})
module.exports = app