-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
65 lines (47 loc) · 2.11 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//___________________
//Dependencies
//___________________
require('dotenv').config()
const express = require('express')
const methodOverride = require('method-override');
const mongoose = require ('mongoose');
const app = express()
const path = require ('path')
const { logger } = require('./middleware/logger')
const errorHandler = require('./middleware/errorHandler')
const cookieParser = require('cookie-parser')
const cors = require('cors')
const corsOptions = require('./config/corsOptions')
const connectDB = require ('./config/dbConn')
const { logEvents } = require('./middleware/logger')
const PORT = process.env.PORT || 3500
console.log(process.env.NODE_ENV) // *learned from Dave Gray's tutorial (see below)
connectDB() // *learned from Dave Gray's tutorial (see below)
app.use(logger) // *learned from Dave Gray's tutorial (see below)
app.use(express.json()) // returns middleware that only parses JSON
app.use(cookieParser()) // *learned from Dave Gray's tutorial (see below)
app.use(cors(corsOptions))
app.use('/', express.static(path.join(__dirname, 'public'))) // populates req.body with parsed info from forms - if no data from forms will return an empty object {}
app.get("/", (req,res) => {
res.send("hello")
})
//___________________
// Routes
//___________________
//localhost:3000
const recipeController = require ('./controllers/yourpantry.js');
app.use('/yourpantry', recipeController)
//___________________
app.use(methodOverride('_method'));// allow POST, PUT and DELETE from a form
app.use(errorHandler) // *learned from Dave Gray's tutorial (see below)
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB')
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
})
mongoose.connection.on('error', err => {
console.log(err)
logEvents(`${err.no}: ${err.code}\t${err.syscall}\t${err.hostname}`,
'mongoErrLog.log')
}) // *learned from Dave Gray's tutorial (see below)
//The above code with * was learned from the MERN stack project tutorial by Dave Gray:
//https://www.youtube.com/watch?v=CvCiNeLnZ00&list=PL0Zuz27SZ-6P4dQUsoDatjEGpmBpcOW8V&index=14