-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
197 lines (177 loc) · 5.43 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
const jwt = require('jsonwebtoken');
const express=require('express');
const app =express();
const bodyParser=require('body-parser')
const port_server = 3000;
const {user_modal,property_modal}=require('./mongoDBcon.js')
const session = require('express-session');
const cookieParser=require('cookie-parser')
// cookie-parser
app.use(cookieParser());
// Use express-session middleware with MongoDB as the session store
app.use(
session({
secret: 'your-secret-key',
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000, // 24 hours in milliseconds
httpOnly: true,
},
})
);
// Allowing body parsing
app.use(bodyParser.urlencoded({ extended: true }));
// set the template engine
app.set('view engine','ejs');
// for serving static content
app.use(express.static('./public'));
// Running a server
app.listen(port_server,()=>{console.log(`Server running on port ${port_server} ! `)});
// get for home page
app.get('/',(q,r)=>{
r.render('index');
});
// get for home page
app.get('/index',(q,r)=>{
r.render('index');
});
// get for login
app.get('/login',(q,r)=>{
console.log("into login");
try {
if (jwt.verify(q.cookies.jwt,'your-secret-key')){
r.redirect('workspace_page');
}
} catch (error) {
console.log("Coookie not found or incorrect JWT token");
r.render('login');
}
});
// get for contact_us
app.get('/contact_us',(q,r)=>{
r.render('contact_us');
});
// get for WS_sell
app.get('/WS_sell',async(q,r)=>{
console.log("\t\t\tinto WS_sell\n\n")
try {
await jwt.verify(q.cookies.jwt,'your-secret-key');
r.render('WS_sell')
} catch (error) {
console.log(error)
r.redirect('login');
}
});
// get for signup
app.get('/signup',(q,r)=>{
r.render('signup');
});
// get for workspace
app.get('/workspace_page',async(q,r)=>{
try {
console.log("\t\t\tWS_page\n\n");
console.log(q.cookies._id);
if (q.cookies && q.cookies.jwt) {
console.log("Cookies:", q.cookies.jwt);}
console.log(q.cookies.jwt);
await jwt.verify(q.cookies.jwt,'your-secret-key');
const properties = await property_modal.find({});
r.render('workspace_page',{properties})
} catch (error) {
console.log(error);
r.redirect('login');
}
//r.render('workspace_page');
});
// get for logout
app.get('/logout', (q, r) => {
q.session.destroy((err) => {
if (err) {
console.error(err);
r.status(500).send('Internal Server Error');
} else {
r.clearCookie('jwt');
r.redirect('/');
}
});
});
// POST for Login
app.post('/login_post', async (q, r) => {
const email_check = q.body.email;
const pass_check = q.body.password;
const user = await user_modal.findOne({ email: email_check });
if (user.password === pass_check) {
// Create a JWT token
const token =await jwt.sign({ email: user.email }, 'your-secret-key', {
expiresIn: '24h',
});
await r.cookie('jwt', token, { httpOnly: true });
await r.cookie('_id',email_check);
r.redirect('workspace_page');
} else {
console.log("\t\t\tProblem in login")
r.status(404);
r.render('login');
}
});
// POST for signup
app.post('/workspace_page',async(q,r)=>{
console.log("\t\t\tInto signup\n");
//fteching data
const email=await q.body.email;
const first_name=await q.body.first_name;
const last_name=await q.body.last_name;
const pass=await q.body.password;
console.log(email,first_name,last_name,pass);
const res=await user_modal.insertMany({
first_name_user:first_name,
last_name_user:last_name,
password:pass,
email:email
});
r.redirect('login');
});
// POST for deletion of property
app.post('/delete_property',async (q,r)=>{
console.log("\t\t\t Into the delete_property")
const data_for_deletion=await {
State:q.body.removeState,
City:q.body.removeCity,
House_number_and_residency_name:q.body.removeUniqueID
};
try {
await property_modal.deleteMany({
State:q.body.removeState,
City:q.body.removeCity,
House_number_and_residency_name:q.body.removeUniqueID
});
r.redirect('workspace_page');
} catch (error) {
r.status(410).redirect('WS_sell');
}
})
// POST for WS_sell
app.post('/WS_sell',async (q,r)=>{
// getting data from page
const data_from_page=await {
contact_person_full_name : q.body.Name_person,
State : q.body.State,
City : q.body.city_name,
House_number_and_residency_name : q.body.Unique_id,
Full_address : q.body.Address1+q.body.Address2+q.body.Address3,
Img_url : q.body.Img_url,
Total_size:q.body.size,
Additional_info: q.body.additional_info,
Price : q.body.Price,
ZIP_code : q.body.zip_code,
BHK : q.body.BHK,
phone_number_for_contact : q.body.phoneNumber
};
await property_modal.insertMany(data_from_page);
r.redirect('/workspace_page');
});
// Page or resource not found
app.get('*',(q,r)=>{
r.status(404).send('<h1>404 Not Found</h1>');
});