-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
160 lines (126 loc) · 5.26 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
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
const express = require("express");
const exphbs = require("express-handlebars");
//require('dotenv').config();
const mysql = require("mysql2/promise");
const session = require("express-session");
const body_parser = require("body-parser");
const bcryptjs = require("bcryptjs");
const flash = require("connect-flash");
const file_upload = require("express-fileupload");
const MySQLStore = require("express-mysql-session")(session);
const request_ip = require("request-ip");
require("dotenv").config({path: "config/keys.env"});
const fetch = require("node-fetch");
const helper = require("./config/helpers.js");
const MySQL_DB = require("./config/MySQL_DAO.js");
const MySQL_DB_pool = require("./config/MySQL_DAO_pool.js");
const User = require("./models/POJO/User.js");
const Customer = require("./models/POJO/Customer.js");
const Employee = require("./models/POJO/Employee.js");
//const Testing_Controller = require("./controllers/Testing_ctrl.js"); //FOR TESTING ONLY!
const General_Controller = require("./controllers/General_ctrl.js");
const Customer_Controller = require("./controllers/Customer_ctrl.js");
const Employee_Controller = require("./controllers/Employee_ctrl.js");
const Product_Controller = require("./controllers/Product_ctrl.js");
const Authentication_Controller = require("./controllers/Authenticate_ctrl.js");
const {is_authorized_customer,is_authorized_employee} = require("./middleware/Authorize_mw.js");
const is_auth = require("./middleware/Authenticate_mw.js");
const User_model = require("./models/MYSQL_models/User_mdl.js");
const http_req = require("./middleware/Http_method_handler_mw.js");
const Category_model = require("./models/MYSQL_models/Category_mdl.js");
//const paypal = require('@paypal/checkout-server-sdk');
const app = express();
/*<div class="form-control">
<label>Gender</label>
Male
<input type="radio" {{#ifEq user.gender "Male" }} checked {{/ifEq}} id="male" name="gender" value="Male">
Female
<input type="radio" id="female" {{#ifEq user.gender "Female" }} checked {{/ifEq}} name="gender" value="Female">
<div class="error">{{errors.gender}}</div>
</div>*/
app.engine("handlebars",helper.engine);
app.set("view engine","handlebars");
app.use(express.static("public"));
//app.use(body_parser.urlencoded({limit: '50mb', extended: true }));
app.use(body_parser.urlencoded({extended: true}));
app.use(body_parser.json({extended: true}));//{limit: '50mb', extended: true}));
app.use(http_req);
//app.use(fetch);
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(flash());
app.use(request_ip.mw());
//For Handlebar access
app.use((req,res,next)=>{
res.locals.user_info = req.session.user_info;
res.locals.message = req.flash("message");
res.locals.cart_info = req.session.cart_info;
res.locals.cart_item_total = req.session.cart_item_total;
res.locals.order_id = req.session.order_id;
res.locals.order_info = req.session.order_info;
//res.locals.categories_info = req.session.categories_info;
//res.locals.categories_info =
next();
})
app.use(file_upload({
parseNested: true, //Needed for submitting input arrays while form enctype is multipart! Array index must be explicity assigned as well
limits: { fileSize: 5 * 1024 * 1024 },
//abortOnLimit: true
}));
//app.locals.stock_val = (sell_price,quantity)=>(parseFloat(sell_price) * parseFloat(quantity));
app.all("/*",(req,res,next)=>{
Category_model.get_all_categories()
.then((categories)=>{
res.locals.categories = categories;
next();
})
.catch(err=>`Error in Server: Error on all routes: ${err}`);
})
//app.use("/",Testing_Controller); //FOR TESTING ONLY!
app.use("/",General_Controller);
app.use("/customer",is_auth,is_authorized_customer,Customer_Controller);
app.use("/employee",is_auth,is_authorized_employee,Employee_Controller);
app.use("/products",Product_Controller);
app.use("/auth",Authentication_Controller);
/*app.get("/",function(req,res){
res.render("general/home",{
title: "Home",
html_id: "home_page_html",
body_id: "home_page_body",
main_id: "home_page_main",
main_class: ["scroll_snap_start_pos "],
home: true,
home_active_link: "active_link",
best_sellers: ["Electronics","Food"],
trial: "<h1>LETS GO</h1>"
});
//res.send("HOME 2");
});
app.get("/signup",function(req,res){
res.render("general/signup",{
title: "Signup",
html_id: "signup_page_html",
body_id: "signup_page_body",
main_id: "signup_page_main",
home_active_link: "active_link",
});
//res.send("HOME 2");
});*/
const User1 = new User;
User1.first_name = "John";
const Employee1 = new Employee;
console.log(`User ${User1.first_name} Employee ${Employee1.first_name}`);
app.listen(process.env.PORT, function(){
//console.log(`SQL END LOG`,MySQL_DB);
//MySQL_DB.destroy();
console.log("Server is connected and running", this.address().port, app.settings.env);
//MySQL_DB.init();
MySQL_DB_pool.init();
});
/*app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});*/