-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
203 lines (131 loc) · 4.67 KB
/
server.js
File metadata and controls
203 lines (131 loc) · 4.67 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var expressSession = require('express-session');
mongoose.Promise = global.Promise;
// For development purposes, uncomment this line:
// mongoose.connect('mongodb://localhost/beers');
// For deployment purposes uncomment this line:
mongoose.connect(process.env.MONGOLAB_PUCE_URI ||'mongodb://localhost/beers');
var Beer = require("./models/BeerModel");
var Review= require('./models/ReviewModel');
var app = express();
// for fb authenticate
app.use(expressSession({ secret: 'mySecretKey' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json()); // This is the type of body we're interested in
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('public'));
app.use(express.static('node_modules'));
// Change the callbackURL you see below
// every time that youre developing locally
// vs deploying to heroku domain
passport.use(new FacebookStrategy({
clientID: '1886152074941466',
clientSecret: '1d9904d87a3e1b3a0b46692cadcc26ef',
// For development
// callbackURL: "http://localhost:8000/auth/facebook/callback",
// For Deployment
callbackURL: "https://letsamp.herokuapp.com/auth/facebook/callback",
profileFields: ['id', 'displayName', 'photos', 'email']
},
function(accessToken, refreshToken, profile, done) {
console.log("accessToken:");
console.log(accessToken);
console.log("refreshToken:");
console.log(refreshToken);
console.log("profile:");
console.log(profile);
return done(null, profile);
}
));
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user);
});
// used to deserialize the user
passport.deserializeUser(function(user, done) {
done(null, user);
});
// To see just the JSON that facebook returns back,
// simply uncomment this line
// route for showing the profile page
app.get('/profile', function(req, res) {
console.log(req.user);
res.render('profile.ejs', {
user: req.user // get the user out of session and pass to template
});
});
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/facebookCanceled'
}));
app.get('/profile', function(req, res) {
res.json(req.user);
});
app.get('/facebookCanceled', function(req, res) {
res.send("fail!");
});
// app.use('/userPage', facebookAuthenticate(req, res, next));
// app.get('/userPage', fucntion(req, res){
// res.send('userPage.html')
// })
app.get('/', function (req, res) {
res.send("You are inside the fullstack project")
});
app.get('/beers', function (req, res) {
Beer.find(function (error, beers) {
res.send(beers);
});
});
app.post('/beers', function (req, res, next) {
console.log(req.body);
var beer = new Beer(req.body);
beer.save(function(err, beer) {
if (err) { return next(err); }
res.json(beer);
});
});
app.delete('/beers/:id', function (req, res) {
res.send('DELETE request to homepage');
Beer.findByIdAndRemove(req.params.id, function(err) {
if (err) throw err;
// we have deleted the person
console.log('Person deleted!');
});
});
app.post('/beers/:id/reviews/', function(req, res, next) {
// req === {
// date: '1/12/16',
// body: {name: "Daniel", text: "gross"},
// params: {id: 123}
// }
// req.params.id === 123
// req.body === {name: "Daniel", text: "gross"}
// db.beers.findById() cousins with Beer.findById
// Beer is the name of the schema, same way we search through a collection
Beer.findById(req.params.id, function(err, foundBeer) {
//foundBeer is the success funct of the beer we found in the database
// we create a function within the function because once we
// find the beer, we want to create and push a review object
if (err) { return next(err); }
var review = new Review(req.body);
foundBeer.reviews.push(review);
foundBeer.save(function (err, review) {
if (err) { return next(err); }
res.json(review);
});
});
});
// fb auth
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/auth/facebook', passport.authenticate('facebook'));
// // For development, uncomment this line
// app.listen(8000);
app.listen(process.env.PORT || '8000');