-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
178 lines (145 loc) · 4.28 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//___________________
//REQUIREMENTS
//___________________
const express = require('express');
const methodOverride = require('method-override');
const mongoose = require ('mongoose');
const app = express ();
const db = mongoose.connection;
const mongoURI = "mongodb://localhost 27017/"+"gameOfPhones";
const MONGODB_URI = process.env.MONGODB_URI;
//console.log(process.env);
const bcrypt = require("bcrypt");
const session = require("express-session")
const Phone = require("./models/phones.js");
const User = require("./models/users.js")
//___________________
//PORT
//___________________
const PORT = process.env.PORT || 3000;
//___________________
//THE MONGOD
//___________________
//That mongoose connection
mongoose.connect(MONGODB_URI , { useNewUrlParser: true }, () => {
console.log("Someday we'll find it, the mongoose connection...");
});
// Fix Depreciation Warnings from Mongoose*
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
// Error / Success
db.on('error', (err) => console.log(err.message + ' is Mongod not running?'));
db.on('connected', () => console.log('mongo connected: ', MONGODB_URI));
db.on('disconnected', () => console.log('mongo disconnected'));
//___________________
//MIDDLEWARE
//___________________
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));// extended: false - does not allow nested objects in query strings
app.use(express.json());// returns middleware that only parses JSON - may or may not need it depending on your project
app.use(methodOverride("_method"));// allow POST, PUT and DELETE from a form
app.use(session({
secret: process.env.SECRET,
resave: true,
saveUninitialized: false,
}));
//___________________
// ROUTES
//___________________
/*home*/
app.get("/", (req, res) => {
if (typeof req.session === "undefined") {
res.render("home.ejs");
} else {
res.render("home.ejs", {
currentUser: req.session.currentUser
});
};
});
/*new*/
app.get("/phones/new", (req,res) => {
if (typeof req.session === "undefined") {
//console.log(typeof req.session);
res.render("new.ejs");
} else {
// console.log("req.session is",typeof req.session);
// console.log("This is session", req.session);
res.render("new.ejs", {
currentUser: req.session.currentUser
});
}
});
/*create*/
app.post("/phones", (req, res) => {
Phone.create(req.body, (error, newPhone) => {
//console.log(req.body);
res.redirect("/phones");
});
});
/*index*/
app.get("/phones", (req, res) => {
//console.log("index route", req.session.currentUser);
Phone.find({}, (error, collection) => {
if (typeof req.session === "undefined") {
res.render("index.ejs", {
phones:collection,
});
} else {
res.render("index.ejs", {
phones:collection,
currentUser: req.session.currentUser
});
};
});
});
/*show*/
app.get("/phones/:id", (req, res) => {
//console.log("show route", req.session);
Phone.findById(req.params.id, (error, foundPhone) => {
if (typeof req.session === "undefined") {
res.render("show.ejs", {
phone:foundPhone
});
} else {
res.render("show.ejs", {
phone:foundPhone,
currentUser: req.session.currentUser
});
};
});
});
/*edit*/
app.get("/phones/:id/edit", (req, res) => {
Phone.findById(req.params.id, (error, foundPhone) => {
if (typeof req.session === "undefined") {
res.render("edit.ejs", {
phone: foundPhone
});
} else {
res.render("edit.ejs", {
phone: foundPhone,
currentUser: req.session.currentUser
});
};
});
});
/*update*/
app.put("/phones/:id", (req, res) => {
Phone.findByIdAndUpdate(req.params.id, req.body, {new:true}, (error, updatedPhone) => {
res.redirect(`/phones/${req.params.id}`)
});
});
/*delete*/
app.delete("/phones/:id", (req, res) => {
Phone.findByIdAndRemove(req.params.id, (error, data) => {
res.redirect("/phones");
});
});
const userController = require('./controllers/users.js');
app.use('/users', userController);
const sessionsController = require('./controllers/sessions.js');
app.use('/sessions', sessionsController);
//___________________
//LISTENER
//___________________
app.listen(PORT, () => console.log( 'Listening on port:', PORT));