-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (39 loc) · 1.18 KB
/
server.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
44
45
46
47
48
49
// Node dependencies
require('dotenv').config()
const express = require('express')
const routes = require('./routes')
// const path = require('path')
const logger = require('morgan')
const mongoose = require('mongoose')
// HTTP port & Mongo URI
const PORT = process.env.PORT || 3001
const MONGODB_URI =
process.env.MONGODB_URI || 'mongodb://localhost/google_books_db'
// Create Express app and march on!
const app = express()
// Middleware
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// Log server activity.
app.use(logger('dev'))
// Serve static assets on production server.
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
}
// Use app routes.
app.use(routes)
// Connect to database.
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connection
.on('error', console.error.bind(
console, 'An error was encountered connecting to the database.'))
.once('connected', () => {
console.log('Successfully connected to the database.')
})
// Launch server and listen for requests.
app.listen(PORT, () => {
console.log(`🌎 ==> API server running on port ${PORT} ...`)
})