-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (40 loc) · 1.3 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
48
49
const mongoose = require('mongoose')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
require('dotenv').config() // pull in dotenv to access enviroment variables
// Declare environment variables
const hostname = process.env.HOSTNAME
const PORT = process.env.PORT || 3000
// Set the database based on the environment
let database
if (process.env.ENVIRONMENT === 'testing') {
database = process.env.TEST_DATABASE
} else {
database = process.env.DATABASE
}
//Try to connect to the database
mongoose.connect(`mongodb://${hostname}/${database}`, {useNewUrlParser: true})
.then(() => {
console.log(`Connected to ${database} successfully`)
})
.catch((error) => {
console.error('Could not connect to Mongo DB: ', error)
})
// Set middlewares
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(express.json())
//Pull in routes
const user = require('./routes/user')
const authenticateUser = require('./routes/auth')
const todo = require('./routes/todo')
//Use Routes middleware
app.use('/api/user', user)
app.use('/api/user/auth', authenticateUser)
app.use('/api/todo', todo)
//Set app to listen on PORT
const server = app.listen(PORT, () => {
console.log(`App started and listening on port ${PORT}`)
})
module.exports = server