-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (48 loc) · 1.39 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable no-undef */
const config = require('./utils/config')
const express = require('express')
require('express-async-errors')
const cors = require('cors')
const mongoose = require('mongoose')
const helmet = require('helmet')
const app = express()
const logger = require('./utils/logger')
const middleware = require('./utils/middleware')
const blogRouter = require('./controllers/blog')
const userRouter = require('./controllers/user')
const loginRouter = require('./controllers/login')
let dbURL
if (process.env.NODE_ENV === 'development') {
dbURL = config.mongo_url_dev
}
if (process.env.NODE_ENV === 'test') {
dbURL = config.mongo_url_test
}
if (process.env.NODE_ENV === 'production') {
dbURL = config.mongo_url
}
const opts = {
autoIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}
mongoose.connect(dbURL, opts)
const db = mongoose.connection
db.once('open', () => {
logger.info(`Database connected: ${dbURL}`)
})
db.on('error', (error) => {
logger.error(`connection error: ${error}`)
})
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cors())
app.use(express.static('build'))
app.use(helmet())
app.use(middleware.loggingMiddleware)
app.use('/api/blogs', blogRouter)
app.use('/api/users', userRouter)
app.use('/api/login', loginRouter)
app.use(middleware.endPoint404)
app.use(middleware.errorHandler)
module.exports = app