-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (33 loc) · 997 Bytes
/
index.js
File metadata and controls
45 lines (33 loc) · 997 Bytes
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 bodyParser = require('body-parser')
const cors = require('cors')
require('dotenv-safe').config()
const authRoute = require('./routes/auth.js')
const {
MONGODB_URI,
MONGODB_TEST_URI,
PORT,
NODE_ENV,
} = process.env
const app = express()
const env = NODE_ENV || 'development'
const dbToConnect = env === 'test' ? MONGODB_TEST_URI : MONGODB_URI
mongoose
.connect(dbToConnect, {'useNewUrlParser': true})
.catch(handleConnectionError)
mongoose.connection.on('error', logError)
app
.use(cors())
.use(bodyParser.json())
.use('/api/user', authRoute)
app.listen(PORT, () => {
console.log(`Development server available on http://localhost:${PORT}`)
})
module.exports = app
function handleConnectionError(error) {
throw new Error('Could not connect to database with error: ', error)
}
function logError(error) {
throw new Error('Database connection failed while running with error: ', error)
}