-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.js
More file actions
181 lines (181 loc) · 5.39 KB
/
Copy pathexpress.js
File metadata and controls
181 lines (181 loc) · 5.39 KB
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
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/nishkarsh";
let express=require('express');
let server=express();
let path=require('path');
let bodyparser=require('body-parser');
var urlencodedParser=bodyparser.urlencoded({extended:true});
server.set('view engine','ejs');
server.use(express.static(__dirname));
var formidable = require('formidable');
var main ;
var n;
var n1;
server.get('/sign',function(req,res)
{
res.sendFile(__dirname+'/sign.html');
});
//creating a new user
server.post('/login',urlencodedParser,function(req,res)
{
if(req.body.usename!==undefined)
{
//creating an object to insert into our database
main = { name: req.body.usename, email: req.body.email,mobile:req.body.mobile,password:req.body.password,
src:"undefined1.png"};
//printing object to check
console.log(main);
//connecting with mongodb and saving in mongodb
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");//database name and collection name
dbo.collection('nishkarsh').insertOne(main, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
}
//login user with name and password
res.sendFile(__dirname+'/login.html');
});
server.post('/portal',urlencodedParser,function(req,res)
{
n=req.body.email1;
//connecting with database and confirming whether usename or password matches
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");
dbo.collection('nishkarsh').findOne({ email:req.body.email1}, function(err, user)
{
if(user===null)
{
res.end("Login invalid");
}
else if (user.email === req.body.email1 && user.password === req.body.password1)
{
res.render('completeprofile',{profileData:user});
}
else
{
console.log("Credentials wrong");
res.end("Login invalid");
}
});
});
/*if(main.name===name && main.password===req.body.password)
{
res.render('completeprofile',{profileData:main});
}
else
{
res.send("error");
}*/
});
//logout funtion
server.get('/info',function(req,res)
{
res.sendFile(__dirname+'/login.html');
});
//update function
server.post('/update',function(req,res)
{
res.sendFile(__dirname+'/update.html');
});
//veryfying username and password for updation of user
server.post('/updation',urlencodedParser,function(req,res)
{
//connecting with database and confirming whether usename or password matches
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");
dbo.collection('nishkarsh').findOne({ name:req.body.username1}, function(err, user) {
if(user===null)
{
res.end("Login invalid");
}
else if (user.name === req.body.username1 && user.password === req.body.password1)
{
res.render('updateprofile',{profileData:user});
}
else
{
console.log("Credentials wrong");
res.end("Login invalid");
}
});
n1={name:req.body.username1};
});
/* if(main.name===req.body.username1 && main.password===req.body.password1)
{
res.render('updateprofile',{profileData:main});
}
else
{
res.send("error");
}*/
});
server.post('/updated',urlencodedParser,function(req,res)
{
main = { name: req.body.name2, email: req.body.emailaddress2,mobile:req.body.mobile2,password:req.body.password2,src:req.body.source };
/*
console.log(main);*/
//printing object to check
console.log(main);
//res.render('completeprofile',{profileData:main});
//connecting with mongodb and saving in mongodb
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");//database name and collection name
var myquery = n1;
var newvalues = {$set: { name: req.body.name2, email: req.body.emailaddress2,mobile:req.body.mobile2,password:req.body.password2,src:req.body.source } };
dbo.collection("nishkarsh").updateOne(myquery, newvalues, function(err, res) {
if (err) throw err;
console.log("1 document updated");
db.close();
});
});
n =req.body.name2;
res.render('completeprofile',{profileData:main});
});
server.post('/delete',function(req,res)
{
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");
var myquery = { name: n };
dbo.collection("nishkarsh").deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log("1 document deleted");
db.close();
});
});
res.sendFile(__dirname+'/clean.html');
});
//search user
server.post('/search',urlencodedParser,function(req,res)
{
var name=req.body.search;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("nishkarsh");
dbo.collection('nishkarsh').findOne({ name:req.body.search}, function(err, user)
{
if(user===null)
{
res.end("Login invalid");
}
else if (user.name === req.body.search)
{
res.render('search2',{profileData:user});
}
else
{
console.log("Credentials wrong");
res.end("Login invalid");
}
});
});
});
server.listen(3000);
console.log('server created');