-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (34 loc) · 1.17 KB
/
index.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
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const app = express()
const mongoose = require('mongoose')
const logger = require('./utils/logger')
const config = require('./utils/config')
const middleware = require('./utils/middleware')
const itemsRouter = require('./controllers/items')
const loginRouter = require('./controllers/login')
const usersRouter = require('./controllers/users')
app.use(express.static('build'))
app.use(express.json())
app.use(cors())
app.use('/api/items', itemsRouter)
app.use('/api/login', loginRouter)
app.use('/api/users', usersRouter)
logger.info(`connecting to ${config.MONGODB_URI}`)
mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
logger.info('connected to MongoDB')
})
.catch((error) => {
logger.error('error connection to MongoDB:', error.message)
})
app.get('/', (request, response) => {
response.send('<h1>Hello World (or you)!</h1>')
})
app.use(middleware.unknownEndpoint)
app.use(middleware.errorHandler)
app.use(middleware.requestLogger)
app.listen(config.PORT, () => {
logger.info(`Server running on port ${config.PORT}`)
})