-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
111 lines (94 loc) · 3.23 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
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var urlParser = bodyParser.urlencoded({ extended: false });
var signup = require('./routes/signup.js');
var search = require('./routes/search.js');
var auth = require('./routes/auth.js');
//Multer Configuration
var multer = require('multer');
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'public/uploads/')
},
filename: function(req, file, cb) {
cb(null, file.originalname)
}
});
var upload = multer({storage: storage});
//Mongoose Configuration
var mongoose = require('mongoose');
mongoose.connect('mongodb://aaronmarkle:[email protected]:51595/surflessons');
//Passport Sessions Configuration
var passport = require('passport');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
var Instructor = require('./models/instructor.js');
passport.deserializeUser(function(id, done) {
Instructor.findById(id, function(err, user) {
done(err, user);
});
});
app.use(require('express-session')({
secret: 'super secret session key',
resave: false,
saveUninitialized: true
}));
var theInitializer = passport.initialize();
app.use(theInitializer);
app.use(passport.session());
//Static Routes
app.use(express.static('public'));
app.use('/search', jsonParser, search);
app.use('/signup', jsonParser, signup);
app.get('/login', urlParser, auth);
app.get('/dashboard', function(req, res) {
if (req.user) {
res.sendFile(__dirname + '/public/dashboard.html');
} else {
res.redirect('/');
}
});
app.get('/userinfo', function(req, res) {
res.json(req.user);
});
app.post('/setBio', jsonParser, function(req, res) {
var Instructor = require('./models/instructor.js');
Instructor.findOneAndUpdate({email: req.user.email}, {bio: req.body.bio}, function(err, user) {
if (err) {
res.send({bioError: 'There was an erorr updating your bio, please try again later.'});
} else {
res.send({bioMessage: 'Your biography has been successfully updated.'});
}
});
});
app.post('/setTimes', jsonParser, function(req, res) {
var Instructor = require('./models/instructor.js');
Instructor.findOneAndUpdate({email: req.user.email}, {availableTimes: req.body.availableTimes}, function(err, user) {
if (err) {
res.send({timesError: 'There was an error updating your availability, please try again later.'});
} else {
res.send({timesMessage: 'Your availability has been successfully updated.'});
}
});
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.post('/upload', upload.single('file'), function(req, res) {
var formattedPath = req.file.path.split('\\').pop().split('/').pop();
var Instructor = require('./models/instructor.js');
Instructor.findOneAndUpdate({email: req.user.email}, {picture: formattedPath}, function(err, user) {
if (err) {
res.send({imageError: 'There was an error updating your profile picture, please try again later.'});
} else {
res.send({imageMessage: 'Your profile picture has been successfully updated.'});
}
});
})
app.listen((process.env.PORT || 8080), function(){
console.log('server live on port 8080');
});