-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
107 lines (91 loc) · 2.88 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
//MODULES
//=========================================
pry = require('pryjs')
var express = require('express');
var mongoose = require('mongoose');
var logger = require('morgan');
var bodyParser = require('body-parser');
var passport = require('passport');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var methodOveride = require('method-override');
var LocalStrategy = require('passport-local').Strategy;
mongoose.Promise = global.Promise;
var port = process.env.PORT || 3000;
var app = express();
var path = require('path');
//EXTERNAL FILES
//=================================
var User = require('./models/user.js');
var UsersController = require('./controllers/users.js');
var HelperController = require('./controllers/helpers.js');
//DATABASE CONNECTION
//=================================
var db = mongoose.connection;
var mongoURI = process.env.MONGODB_URI || 'mongodb://localhost/grocery-list';
mongoose.connect(mongoURI);
// MIDDLEWARE / CONFIGURATION
// ==================================
app.use(express.static(path.join(__dirname,'public')));
mongoose.Promise = global.Promise;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(logger('dev'));
//PASSPORT
//=================================
app.use(cookieParser('keyboard cat'));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
//To be used when user is created ********
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//MIDDLEWARE ROUTING
//=================================
app.use("/user", UsersController);
app.use("/helper", HelperController);
//USER HOME REGISTER
//=================================
app.get('/', function(req, res){
res.render('index');
});
//USER HOME REGISTER
//=================================
app.post('/register', function(req, res){
console.log("USER REGISTRATION INFORMATION >>>>>", req.body.username);
User.register(new User({
username: req.body.username
}),
req.body.password,
function(err, user){
console.log("AFTER REGISTRATION USER >>>>>>>>", user);
req.login(user, function(err){
if (err) {console.log(err); }
return res.json(user);
});
});
});
//USER HOME LOGIN
//=================================
app.post('/login', passport.authenticate('local'), function(req, res){
res.json(req.user);
});
//USER HOME LOGOUT
//=================================
app.delete('/logout', function(req, res){
console.log("USER IS LOGGING OUT!!!!!!!");
req.logout();
res.send("USER IS LOGGED OUT")
});
//PORT LISTENER
//=========================================
app.listen(port, function() {
console.log('=======================');
console.log('RUNNING on PORT ' + port);
console.log('=======================');
});