-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (62 loc) · 1.88 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
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
74
75
76
// Require local models & controllers
const Review = require('./models/review')
const Comment = require('./models/comment')
const movies = require('./controllers/movies')
const reviews = require('./controllers/reviews')
const comments = require('./controllers/comments')
/*
const admin = require('./controllers/admin') //initialize admin
*/
// Require other npm features.
// Here, mongoose interacts with the mongodb database.
const mongoose = require('mongoose')
// Here, express deals with various CRUD operations.
const express = require('express')
const expressHbs = require('express-handlebars')
// Here, other important packages are added.
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
// Initialize express app.
const app = express()
// Set up handlebars
app.engine('hbs', expressHbs({
'defaultLayout': 'main',
'extname': '.hbs',
'helpers': {
json: (context) => (JSON.stringify(context, null, '\t')),
equals: (context, value) => (context === value),
join: (string, value) => ([string, value].join('')),
}
}))
app.set('view engine', 'hbs')
// Connect to mongoose.
const connectionString = process.env.MONGODB_URI ?? 'mongodb://localhost:27017/rotten-potatoes'
const port = process.env.PORT ?? 3000
mongoose.connect(connectionString, {
'useNewUrlParser': true,
'useUnifiedTopology': true,
'useFindAndModify': false,
})
// Serve static files from the public folder.
app.use(express.static('public'))
// Override with operations like those with DELETE or PUT.
app.use(methodOverride('_method'))
// This parses the request data under req.body into objects.
app.use(bodyParser.urlencoded({extended: true}))
// ROUTES
movies(app)
reviews(app)
comments(app)
/*
admin(app)
*/
// LISTEN
if (require.main === module) {
app.listen(port, () => {
console.info(
`App listening on port ${port}!`
+ '\nhttp://localhost:3000/'
)
})
}
module.exports = app