-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (53 loc) · 1.83 KB
/
server.js
File metadata and controls
73 lines (53 loc) · 1.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const expressLayouts = require('express-ejs-layouts')
/**
* import the routes from our routes controller
*/
const indexRouter = require('./routes/index')
const authorRouter = require('./routes/authors')
/**
* install and use body-parser to make it easy to read from a server
* then remember to tell express how to use that library using app.use()
*/
const bodyParser = require('body-parser');
/**
* set our view engine
* set where the views will be coming from
* __dirname means take the current directory name
* hook up express layouts and put it in a layouts folder
* this is to prevent replicating all the beginning html of our project
* also tell express where your static files will be
* finally tell your app you want to listen in a certain port
*/
app.set('views', __dirname + '/views')
app.set('view engine','ejs')
app.set('layout', 'layouts/layout')
app.use(expressLayouts)
app.use(express.static('public'))
app.use(bodyParser.urlencoded({limit: '10mb', extended: false}))
/**
* setting up DB and connecting to MongoDB locally
* Remember sto set up the database environment
* Declare the PORT variable
*/
const mongoose = require('mongoose')
const PORT = process.env.PORT
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true ,
useUnifiedTopology: true}
)
/** Log if we are now connected to the db or not */
const db = mongoose.connection
db.on('error', error => console.error(error))
db.once('open', () => console.log('Connected to Mongoose!'))
app.use('/', indexRouter)
app.use('/authors', authorRouter)
app.listen(PORT || 3000);
/**
* Note: you dont want to ever hardcode your ports or routes to the DB
* use process.env.PORT or process.env.DATABASE_URL
*/