-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (37 loc) · 1.19 KB
/
app.js
File metadata and controls
44 lines (37 loc) · 1.19 KB
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
import express from 'express'
import cors from 'cors'
import mongoose from 'mongoose'
import logger from './utils/logger.js'
import config from './utils/config.js'
import BlogsRouter from './controllers/blogs.js'
import UsersRouter from './controllers/users.js'
import LoginRouter from './controllers/login.js'
import TestRouter from './controllers/tests.js'
import { requestLogger, unknownEndpoint, errorHandler, getToken, getUser } from './utils/middleware.js'
const app = express()
mongoose
.connect(config.URI)
.then(() => {
logger.info('connection to mongodb succeed')
})
.catch((error) => {
logger.error('connection to mongodb failed', error)
})
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'))
app.use(cors())
app.use(express.static('dist'))
app.use(express.json())
app.use(requestLogger)
app.use(getToken)
app.use('/api/users', UsersRouter)
app.use('/api/login', LoginRouter)
app.use('/api/blogs', getUser, BlogsRouter)
app.get('/health', (req, res) => {
res.send('ok')
})
if (process.env.NODE_ENV === 'test') {
app.use('/api/tests', TestRouter)
}
app.use(unknownEndpoint)
app.use(errorHandler)
export default app