-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (105 loc) · 2.94 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// EXPRESS BOILERPLATE
// DEPENDENCIES
require('dotenv').config();
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const Fruit = require('./models/fruits.js');
const methodOverride = require("method-override");
// DATABASE CONNECTION
// How to connect to the database either via heroku or locally
const MONGODB_URI = process.env.MONGODB_URI;
mongoose.set("strictQuery",true);
mongoose.connect(MONGODB_URI,{
useNewUrlParser:false,
})
// Mongo error/success
const db = mongoose.connection
db.on('error', (err) => console.log(`${err.message} MongoDB Not Running!`))
db.on('connected', () => console.log('mongo connected'))
db.on('disconnected', () => console.log('mongo disconnected'))
// MIDDLEWARE
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(methodOverride("_method"));
app.use(express.static('public'));
// ROUTES - INDUCES
//HOME
app.get('/', (req, res)=>{
// render some text
res.send("home page")
});
//INDEX
app.get('/fruits/', (req, res) => {
Fruit.find({}, (error, allFruits) => {
res.render('index.ejs', {
fruits: allFruits,
})
})
});
// //NEW
// app.get('/fruits/new', (req, res) => {
// res.render('new.ejs');
// });
// //DELETE
// app.delete("/fruits/:id", (req, res) => {
// Fruit.findByIdAndRemove(req.params.id, (err, data) => {
// res.redirect("/fruits")
// })
// });
// //UPDATE
// app.put("/fruits/:id", (req, res) => {
// if (req.body.readyToEat === "on") {
// req.body.readyToEat = true
// } else {
// req.body.readyToEat = false
// }
// Fruit.findByIdAndUpdate(
// req.params.id,
// req.body,
// {
// new: true,
// },
// (error, updatedFruit) => {
// res.redirect(`/fruits/${req.params.id}`)
// }
// )
// })
// //CREATE
// app.post('/fruits/', (req, res) => {
// if (req.body.readyToEat === 'on') { //if checked, req.body.readyToEat is set to 'on'
// req.body.readyToEat = true;
// } else { //if not checked, req.body.readyToEat is undefined
// req.body.readyToEat = false;
// }
// Fruit.create(req.body, (error, createdFruit) => {
// if (error) {
// console.log(error);
// res.send(error);
// }
// else {
// res.redirect('/fruits');
// }
// });
// });
// //EDIT
// app.get("/fruits/:id/edit", (req, res) => {
// Fruit.findById(req.params.id, (error, foundFruit) => {
// res.render("edit.ejs", {
// fruit: foundFruit,
// })
// })
// })
// //SHOW
// app.get('/fruits/:id', (req, res) => {
// Fruit.findById(req.params.id, (err, foundFruit) => {
// res.render('show.ejs', {
// fruit: foundFruit
// });
// });
// });
// PORT
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});