forked from TeamSadbhawna/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
211 lines (180 loc) · 4.27 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
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
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const port = process.env.PORT ||3000;
//bcrypt package
const bcrypt = require('bcrypt');
const saltRounds = 10;
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json());
app.use(express.static("public"));
// set the view engine to ejs
app.set('view engine', 'ejs');
//schema for useNewUrlParser
const userSchema = new mongoose.Schema({
name:
{
type:String,
required:[true,"Please enter your Name"]
},
email: {
type: String,
required: [true,"Please enter your mail-ID"]
},
password:
{
type:String,
required: [true,"Please enter a password"]
}
});
const User = new mongoose.model("User", userSchema);
// Schema for resourcedb
const resourceSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Please enter your full-name"]
},
address: {
type: String,
required: [true, "Please enter your Home-Address"]
},
state: {
type: String,
required: [true, "Please enter from which state you are"]
},
city: {
type: String,
required: [true, "Please enter your City"]
},
district: {
type: String,
required: [true, "Please enter the district"]
},
pincode: {
type: Number,
required: [true, "Please enter the pin-code"]
},
email: {
type: String,
required: [true, "Please enter your email address"]
},
contact: {
type: Number,
required: [true, "Please enter your phnone-no"]
},
description: {
type: String,
required: [true, "Description of the things are required"]
},
rating: {
type: Number,
required: [true, "Please enter the rating "],
min: 1,
max: 10
}
});
const Resource = mongoose.model("Resource", resourceSchema);
app.get("/", function(req, res) {
res.render("index");
});
//login authentication
app.get("/login", function(req, res) {
res.render("login");
});
app.post("/login", function(req, res) {
const username = req.body.email;
const password = req.body.password;
User.findOne({
email: username
}, (err, foundUser) => {
if (err) {
console.log(err);
} else {
if (foundUser) {
bcrypt.compare(password, foundUser.password, function(err, result) {
if (result === true) {
res.render("donateform")
}
});
}
}
})
})
//signup authentication and retreiving user data
app.get("/signup", function(req, res) {
res.render("signup");
});
app.post("/signup", function(req, res) {
if(req.body.cnfpassword!="" && req.body.password!="" && req.body.fullname!="")
{
bcrypt.hash(req.body.cnfpassword, saltRounds, function(err, hash) {
const newUser = new User({
name:req.body.fullname,
email: req.body.email,
password: hash
});
newUser.save((err) => {
if (err) {
res.render("failure");
} else {
res.render("donateform");
}
});
});
}
else
{
res.render("failure");
}
})
app.get("/forgotpwd", function(req, res) {
res.render("forgotpwd");
});
app.get("/donate", function(req, res) {
res.render("login");
});
app.get("/resources", function(req, res) {
Resource.find({}, function(err, foundItems) {
res.render("resources", {
newItemsList: foundItems,
});
});
});
app.post("/donate", function(req, res) {
const dname = req.body.fullname;
const daddress = req.body.address;
const dstate = req.body.state;
const dcity = req.body.city;
const ddistrict = req.body.district;
const dpincode = req.body.pincode;
const demail = req.body.email;
const dcontact = req.body.contact;
const ddescription = req.body.description;
const drating = req.body.rating;
const data = new Resource({
name: dname,
address: daddress,
state: dstate,
city: dcity,
district: ddistrict,
pincode: dpincode,
email: demail,
contact: dcontact,
description: ddescription,
rating: drating,
});
data.save();
res.redirect("/resources");
});
app.listen(port, function() {
console.log(`Server is running on port ${port}`);
});