-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
76 lines (65 loc) · 1.95 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
var express = require('express');
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');
var cookieParser = require('cookie-parser');
var flash = require('express-flash');
var MongoStore = require('connect-mongo/es5')(session);
var passport = require('passport');
var path = require('path');
var secret = require('./config/secret');
var User = require('./models/user');
var Category = require('./models/category');
var cartLength = require('./middleware/middleware');
var app = express();
mongoose.connect( secret.database, function(err) {
if(err) {
console.log(err);
} else {
console.log("Connected to the database");
}
});
//Middleware
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 })
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
app.use(cartLength);
app.use(function(req, res, next){
Category.find({}, function(err, categories){
if(err) return next(err);
res.locals.categories = categories;
next();
})
});
app.engine('ejs', engine);
app.set('view engine', 'ejs');
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);
app.use('/api', apiRoutes);
app.listen(secret.port, function(err) {
if (err) throw err;
console.log("Server is Running on port " + secret.port );
});