-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
167 lines (131 loc) · 3.93 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
const express=require('express');
const bodyParser=require('body-parser');
const ejs=require('ejs');
const encrypt=require('mongoose-encryption');
const mongoose =require('mongoose');
var app=express();
app.set("view engine", "ejs");
app.use(express.urlencoded({extended:true}));
app.use(express.static('public')); // this shows that all static files are placed in the public folder...
mongoose.connect("mongodb://127.0.0.1:27017/Gamestore"); //--> connecting with mongoose here..
const proSchema =new mongoose.Schema({
imagePath:{type:String,required:true},
title:{type:String,required:true},
price:{type:Number,required:true}
});
const trySchema = new mongoose.Schema({
name:{type:String,require:true},
email:{type:String,require:true},
password:{type:String,require:true},
});
//Encryption done here...----->Written below schema
const sec="thisislittlesecret";
trySchema.plugin(encrypt,{secret:sec,encryptedFields:["password"]});
const item=mongoose.model("seconds",trySchema);
const Product=mongoose.model("products",proSchema);
//module.exports=Product;
var products= [
new Product({
imagePath:'/img/eotb2.jpg',
title:'Eye of Beholder 2',
price:300
}),
new Product({
imagePath:'/img/call.jpeg',
title:'Call of Duty',
price:350
}),
new Product({
imagePath:'/img/castlefall.jpg',
title:'Castle Fall',
price:200
}),
new Product({
imagePath:'/img/Darkness.jpg',
title:'Darkness And Flame',
price:900
}),
new Product({
imagePath:'/img/dark-souls.jpg',
title:'Dark Souls 2',
price:1150
}),
new Product({
imagePath:'/img/goldNblood.jpeg',
title:'Gold N Blood',
price:300
}),
new Product({
imagePath:'/img/Hellwomb.jpg',
title:'Hellwomb',
price:800
})
];
//var done=0;
for(let i=0;i<products.length;i++){
products[i].save();/*.then(()=>{
done++;
if(done===products.length){
exit();
}
}).catch((err)=>{
console.log(err);
});*/
}
/*function exit(){
mongoose.disconnect();
}*/
app.get("/",(req,res)=>{
res.render("home");
});
app.post("/register",(req,res)=>{
const foundUser= new item({ //--foundUser is the new User added...
name:req.body.username,
email: req.body.emailname,
password: req.body.password
});
foundUser.save();
res.render("user",{foundUser});
});
app.post("/login",(req,res)=>{
const username=req.body.username;
const password= req.body.password;
item.findOne({email:username}).then(function(foundUser){
// const pername=foundUser.name; --- this will show you the User name in console...
if(foundUser){
if(foundUser.password===password){
//console.log(pername);
res.render("user",{foundUser});
}else{
res.send("<h1>Invalid Username or Password!!</h1>");
}
}
}).catch((err)=>{
console.log(err);
});
});
app.get("/login",(req,res)=>{
res.render("login");
});
app.get("/register",(req,res)=>{
res.render("register");
});
app.post( "/user",function(req,res){
const itemName= req.body.ele1; //Here ele1 is the (name:"") of the input
Product.findOne({title:itemName}).then(function(foundItems){
const ina=foundItems.title;
const path=foundItems.imagePath;
const prc=foundItems.price;
if(foundItems){
/* console.log(ina);
console.log(path);
console.log(prc);*/
res.render("cart",{foundItems});
}
}).catch(function(err){
console.log(err);
});
});
app.listen(8000,()=>{
console.log("Server Started....");
});