-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
175 lines (127 loc) · 4.66 KB
/
Copy pathserver.js
File metadata and controls
175 lines (127 loc) · 4.66 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
var express = require('express');
//logs rquests and errors in the terminal and the amount
//of time it takes to get that page
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var engine = require('ejs-mate');
var session = require('express-session');
// session id is an encryption signature
// session is stored on the server
// session doesn't persist after browser is closed
var cookieParser = require('cookie-parser');
// users cookies to store a session id in a users browser
// on value cookie to retrieve information stored on the server
// server side storage
// cookies store on the visitors browser
// cookies has sessions encryted in their cookies
var flash = require('express-flash');
var MongoStore = require('connect-mongo/es5')(session);
// this is the db for storing the session
// this will be saved into mongo db
// mongo store is specifically to store sessions on the server side
// mongo store library is depending on expression session
// we need to know that a session will be based on an express
// session library
var passport = require('passport');
// oauth login support
var secret =require('./config/secret');
var User = require('./models/user');
// config files store database information
// passport is a library for authentication
var Category = require('./models/category');
var cartLength = require('./middlewares/middlewares');
var app = express();
// connect mongoose to the db
// 'mongodb://...' is a path to the database
// what went here instead of this?
// we studued just a path to the local host and maybe a route
// connection
mongoose.connect(secret.database, function(err){
if (err) {
console.log(err);
}
else{
console.log("Connected to the Database");
}
});
// middleware.. almost everything you require
// you have to use
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: secret.secretKey,
store: new MongoStore({url: secret.database, autoReconnect: true})
}));
// flash is dependent on session and cookie
app.use(flash());
app.use(passport.initialize());
// acts as a middleware to alter the req object
// and change the user value that is currently the session id
// from the client cookie into the true deserialized user object
app.use(passport.session());
// this function below will have every user able to
// have the user object by default in every route
// without having to specify the object in every route
// why did we not use the universal /* here
app.use(function(req, res, next){
// whats does locals mean- local variable
// user is the name of the object you want to use
res.locals.user = req.user;
next();
});
app.use(cartLength);
app.use(function(req, res, next){
// in the catergories case we have to query it in order to
// store it in the local variable below
// as opposed to the above middlewae for users
// we already the req.user object so we don't have to query it
// we got the req.user made with serialize.. why wasn't category
// serialized ??
Category.find({}, function(err, categories){
// the above means we are finding all the categories in this object
// if you don't put anything in the brackets that means that you want all
// the categories documents in the db
if (err)
return next(err);
// this is storing that list of categories in a local variable named
// categories..... you can name local.___ anything but we name it
// locals.categories
// what is a local variable ??
// we are going to use this local variable we created in the navbar
res.locals.categories = categories;
next();
});
});
app.engine('ejs', engine);
app.set('view engine', 'ejs');
// all route files need to be required in the server
var mainRoutes = require('./routes/main');
var userRoutes = require('./routes/user');
var adminRoutes = require('./routes/admin');
var apiRoutes = require('./api/api');
app.use(mainRoutes);
app.use(userRoutes);
app.use(adminRoutes);
// we are adding 'api' here so that all the apis
// associated w/ this routes file will will be a sub url
// of api. This allows us to avoid prefixing all the api's
// that could come up with api/...
app.use('/api',apiRoutes);
// to see the value of the cookie
app.get('/*', function(req, res, next){
if(typeof req.cookies['connet.sid'] !== 'undefined'){
console.log(req.cookies['connect.sid']);
}
next();
});
app.listen(secret.heroku || secret.port, function(err) {
if (err) throw err;
console.log("Server is Running on Port" + secret.port);
});