-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
227 lines (213 loc) · 6.58 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const express = require("express");
const http = require("http");
const bodyParser = require("body-parser");
const app = express();
const Admin = require("./routes/Admin");
const ProductView = require("./routes/Product");
const Error = require("./routes/ErrorRoute");
const root = require("./utils/path");
const path = require("path");
const Product=require("./models_sequelize/product");
const User=require("./models_sequelize/user");
const Cart=require('./models_sequelize/cart');
const CartItem=require('./models_sequelize/cartItem');
const Order=require("./models_sequelize/order");
const OrderItem=require("./models_sequelize/orderItem");
const sequqlize_routes=require("./routes/Sequelize_routes/Admin");
const userMongoDB=require("./models/mongoModels/Uers");
const {mongoConnectFunction}=require("./utils/mongodb");
const mongooseDB = require("./utils/mongoosedb");
const mongooseRoutes=require("./routes/mongoose_routes/Admin");
const mongooseUser = require("./models/mongooseModels/User");
const session = require("express-session");
const mongoSession = require("connect-mongodb-session")(session);
const csrf=require("csurf");
const nodemailer=require("nodemailer");
const mailTransport = require("nodemailer-sendgrid-transport");
const multer = require("multer");
// const seq_model = require("./models/sequelize_product");
const seq = require("./utils/db_sequelize");
const store=new mongoSession({
uri:"mongodb+srv://Mayank5354:Mayank%[email protected]/myShopMongoose",
collection:"sessions"
})
const csrfProtection = csrf();
const apiKey = "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";
const mailer = nodemailer.createTransport(mailTransport({
auth:{
// service:'gmail',
// auth:{
// user:"[email protected]",
// pass:"Hitesh@Manisha5354"
// }
// api_user:'[email protected]',
api_key:apiKey
}
}));
const fileStorage = multer.diskStorage({
destination : (req,file,cb) => {
cb(null,'images');
},
filename:(req,file,cb) => {
cb(null,file.originalname);
}
});
const filefilter = (req,file,cb) => {
cb(null,true);
}
app.set("view engine", "ejs");
app.set("views", "views");
app.engine("ejs", require("ejs").__express);
app.use(multer({storage:fileStorage}).single("image"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(root, "Public")));
app.use(express.static(path.join(root, "images")));
// app.use((req,res,next)=>{
// User.findByPk(1).then(user =>{
// req.user=user;
// next();
// }).catch(err =>{ console.log(err);});
// });
// same as above but for mongodb
app.use(session({
secret:"Mayank.5354",
resave:false,
saveUninitialized:false,
store:store
}));
app.use(csrfProtection);
app.use((req,res,next) => {
res.locals.csrfToken = req.csrfToken();
next();
})
app.use((req,res,next)=>{
if(!req.session.user){
return next();
}
console.log(req.session);
mongooseUser.findById(req.session.user._id)
.then(user => {
req.user=user;
next();
})
// res.setHeader("Set-Cookie","isLoggedIn=true;Max-Age=1");
// req.isLoggedIn=true;
// req.session.isLoggedIn=true;
// userMongoDB.findByEmailAndPassword("[email protected]","Mayank.5354")
// .then(user =>{
// console.log("User fetched sucessfully");
// console.log(user);
// // req.user= new userMongoDB(user.email,user.password,user.cart,user._id);
// // console.log("req.user",req.user);
// next();
// });
// mongooseUser.findById("659cfb4b5c68710d76819953")
// .then(
// user => {
// req.user= user;
// req.session.user=user;
// console.log("session ",req.session);
// console.log("req.user " , req.user);
// next();
// }
// )
})
app.use("/admin", Admin);
app.use("/Sequelize",sequqlize_routes);
app.use("/mongoose",mongooseRoutes);
app.use(ProductView);
app.use(Error);
// app.use((req,res,next)=>{
// count++;
// console.log("Hello World");
// res.setHeader("Content-Type","text/html");
// res.write("<h2>This is Main Route</h2>");
// res.write(`${count}`);
// res.end();
// })
const server = http.createServer(app);
Product.belongsTo(User,{constraints:true,onDelete:'CASCADE'});
User.hasMany(Product);
User.hasOne(Cart);
Cart.belongsTo(User);
Cart.belongsToMany(Product,{through:CartItem});
Product.belongsToMany(Cart,{through:CartItem});
User.hasMany(Order);
Order.belongsToMany(Product,{through:OrderItem});
seq.sync({force:true})
.then(e => {
return User.findByPk(1)
})
.then(user =>{
if(!user){
return User.create({name:"Mayank",email:"[email protected]"})
}
return user;
})
.then(user =>{
console.log(user)
var fetchCart=null;
user.getCart().then(cart=>{
if(!cart){
user.createCart().then(e=>{
fetchCart=e;
console.log("cart creatd");
});
}
else{
fetchCart=user.getCart();
}
})
return fetchCart;
})
.then(user =>{
console.log(user);
mongoConnectFunction(()=>{
mongooseDB().then(
result => {
// mailer.sendMail({
// from:"[email protected]",
// to:'[email protected]',
// subject:"Hello Test App",
// html:"<h1>Hi, A mail From ms49</h1>"
// },(error,info) => {
// console.log(info);
// console.log(error);
// })
server.listen(5354);
}
)
// mongooseDB().then(
// (e)=>{
// const user = new mongooseUser({
// name:"Mayank",
// email:"[email protected]",
// cart:{
// items:[]
// }
// })
// user.save().then(result => {
// console.log("user created");
// server.listen(5354);
// })
// }
// )
});
console.log("server created");
})
.catch((e)=>{console.log(e);});
// seq.sync()
// .then(
// result => {
// console.log(result);
// seq_model.create({
// title: "mayank "
// });
// seq_model.findAll()
// .then(
// e=>{
// console.log("data",e);
// console.log(e.length );
// });
// })
// .catch((e) => { console.log(e); })