-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (32 loc) · 995 Bytes
/
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
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const HttpError = require('./models/http-error')
const Routes = require('./Routes/routes')
const CORS = require('cors')
const dotenv = require('dotenv')
dotenv.config()
const app = express()
app.use(bodyParser.json())
app.use(CORS())
app.use('/api', Routes)
app.use((req, res, next) => {
const error = new HttpError('Could not find this route.', 404)
throw error
})
app.use((error, req, res, next) => {
res.status(error.code || 500)
res.json({ message: error.message || 'An unknown error occurred!' })
})
mongoose
.connect(
`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.22ccrab.mongodb.net/?retryWrites=true&w=majority`,
{ dbName: process.env.DB_NAME }
)
.then(() => {
app.listen(process.env.PORT || 5000)
console.log(`Running on port: ${process.env.PORT || 5000}`)
})
.catch(err => {
console.log(err)
})