-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (101 loc) · 3.51 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
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
var express = require('express');
var path = require('path');
var fileUpload = require('express-fileupload');
var session = require('express-session');
var validator = require('express-validator');
var csrf = require('csurf');
var csrfProtection = csrf();
var passport = require('passport');
var flash = require('connect-flash');
var bodyParser = require('body-parser');
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/hotel', { useNewUrlParser: true });
var AmenitiesComponent = require('./models/AmenitiesComponent');
var LocationComponent = require('./models/LocationComponent');
//Init app
var app = express();
require('./config/passport');
//View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(session({secret: 'secret', resave: false, saveUninitialized: false}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
// app.use(function (req, res, next) {
// res.locals.messages = require('express-messages')(req, res);
// next();
// });
app.use(validator());
//Set public folder
app.use(express.static(path.join(__dirname, 'public')));
//Express file uplaod middleware
app.use(fileUpload());
//body parser middleware
//Get all amenities to pass to header.ejs
AmenitiesComponent.findOne({ slug: 'amenities' }, function (err, resultObj) {
if (err) {
console.log(err);
} else {
app.locals.language = 'bg';
app.locals.amenities = resultObj['amenities_' + app.locals.language];
}
});
LocationComponent.findOne({ slug: 'location' }, function (err, resultObj) {
if (err) {
console.log(err);
} else {
app.locals.location = resultObj;
}
});
app.use(bodyParser.urlencoded({ extended: false }));
//parse application/json
app.use(bodyParser.json());
app.locals.errors = null;
// app.get('/', function(req, res) {
// res.send('working');
// })
//Start the server
var port = 3000;
app.listen(port, function () {
console.log('Server started on port ' + port);
});
app.use('/', router.get('/language', function (req, res) {
req.app.locals.language = req.app.locals.language == 'bg' ? 'en' : 'bg';
//todo this is written .. twixe
AmenitiesComponent.findOne({ slug: 'amenities' }, function (err, resultObj) {
if (err) {
console.log(err);
} else {
req.app.locals.amenities = resultObj['amenities_' + req.app.locals.language];
res.redirect('back');
}
});
}));
//set routes
app.get('*', function (req, res, next) {
res.locals.user = req.user || null;
next();
});
var pagesR = require('./routes/pagesR');
var adminSpa = require('./routes/admin_spa');
var adminLangindPagge = require('./routes/admin_landing_page');
var adminSendMessage = require('./routes/admin_contact');
var adminLocation = require('./routes/admin_location');
var adminAboutUs = require('./routes/admin_about_us');
var adminPricing = require('./routes/admin_pricing');
var adminOffert = require('./routes/admin_offert');
var adminImages = require('./routes/admin_images');
var adminUser = require('./routes/admin_user');
// var pages = require('./routes/pages.js');
app.use('/', pagesR);
app.use('/admin', adminLangindPagge);
app.use('/admin', adminAboutUs);
app.use('/admin', adminSpa);
app.use('/admin', adminPricing);
app.use('/admin', adminOffert);
app.use('/admin', adminImages);
app.use('/admin', adminSendMessage);
app.use('/admin', adminLocation);
app.use('/admin', adminUser);