-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (36 loc) · 1.06 KB
/
app.js
File metadata and controls
46 lines (36 loc) · 1.06 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
45
46
const config = require('./utils/config')
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const personRouter = require('./controllers/person')
const Person = require('./models/person')
const cors = require('cors')
const middleware = require('./utils/middleware')
const url = config.MONGO_URI
mongoose.set('strictQuery', false)
console.log('connecting to mongoDB...')
mongoose.connect(url)
.then(result => {
console.log('connected to MongoDB')
})
.catch((error) => {
console.log('error connecting to MongoDB:', error.message)
})
app.use(cors())
app.use(express.json())
app.use(express.static('build'))
app.use(middleware.requestLogger)
/* info */
app.get('/info', (_req, res) => {
Person.find({})
.then(returnedP => {
const info = `
<p>Phonebook has info for ${returnedP.length} people </p>
<p> ${new Date()}</p>`
res.send(info)
})
})
app.use('/api/persons', personRouter)
app.use(middleware.unknownEndpoint)
app.use(middleware.errorHandler)
module.exports = app