-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
36 lines (29 loc) · 1.14 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
// app.js
const express = require('express')
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const booksRouter = require ('./routes/api/books')
const authorsRouter = require ('./routes/api/authors')
const commentRouter = require ('./routes/api/comments')
const usersRouter = require('./routes/api/users');
const cartRouter = require('./routes/api/carts');
require('dotenv').config()
// Create Express app
const app = express()
//Connect to MongoDB
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false})
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('Connected to Database'))
// Bodyparser middleware
app.use(bodyParser.json())
//API Routes
app.use('/api/books', booksRouter)
app.use('/api/authors', authorsRouter)
app.use('/api/users', usersRouter)
app.use('/api/comments', commentRouter)
app.use('/api/carts', cartRouter)
// Get PORT
const PORT = process.env.PORT || 5000
// Start the Express server
app.listen(PORT, () => console.log(`Server started on port ${PORT}`))