-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (36 loc) · 1.16 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
import express from 'express'
import cors from 'cors'
import mongoose from 'mongoose'
import 'express-async-errors'
import * as config from './utils/config'
import blogsRouter from './controllers/blogs'
import defaultRoutesRouter from './controllers/defaultRoutes'
import loginRouter from './controllers/login'
import testingRouter from './controllers/testing'
import usersRouter from './controllers/users'
import * as middleware from './utils/middleware'
import * as logger from './utils/logger'
const app = express()
logger.info('connecting to', config.MONGODB_URI)
mongoose
.connect(config.MONGODB_URI)
.then(() => {
logger.info('connected to MongoDB')
})
.catch((error) => {
logger.error('error connecting to MongoDB:', error.message)
})
app.use(cors())
app.use(express.json())
app.use(middleware.requestLogger)
app.use(middleware.tokenExtractor)
app.use('/api/login', loginRouter)
app.use('/api/blogs', blogsRouter)
app.use('/api/users', usersRouter)
app.use('/', defaultRoutesRouter)
if (process.env.NODE_ENV === 'test') {
app.use('/api/testing', testingRouter)
}
app.use(middleware.unknownEndpoint)
app.use(middleware.errorHandler)
export default app