-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
274 lines (193 loc) · 6.85 KB
/
index.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var http = require('http');
var fs = require('fs');
var url = require('url');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer({ dest: __dirname+ "/public/files/"});
var session = require('express-session');
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.use(session({secret: "Your secret key"}));
var fs = require("fs");
var jsonfile = require('jsonfile')
var data = '';
var mongoose = require('mongoose');
//mongoose.connect('mongodb://127.0.0.1:27017/manu');Manusmiles@619
mongoose.connect('mongodb://manu:[email protected]:39844/mambo');
var personSchema = mongoose.Schema({id: String,password: String});
var users = mongoose.model("users", personSchema);
var dataSchema = mongoose.Schema({name: String,age: String,number:Number,image:String,date:Date});
var detailsdata = mongoose.model("person", dataSchema);
app.set('view engine', 'pug');
app.set('views', './views');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static('public'));
app.use(express.static('images'));
function checkSignIn(req, res,next){
if(req.session.users){
next(); //If session exists, proceed to page
} else {
console.log("user"+req.session.users)
var err = new Error("Not logged in!");
res.redirect('/signup');
console.log(err); //Error, trying to access unauthorized page!
}
}
app.get('/',checkSignIn,function(req, res){
res.sendFile(__dirname+"/form.html");
});
app.get('/signup' ,function(req, res){
if(req.session.users){
res.render('person'); //If session exists, proceed to page
}else
{
res.render('signup');
}
});
app.get('/logout', function(req, res){
req.session.destroy(function(){
console.log("successfully logged out")
res.redirect('/signup');
});
});
app.post('/signup', function(req, res){
if(!req.body.id || !req.body.password){
res.status("400");
res.send("Invalid details!");
}
else{
users.find({"id":req.body.id,"password":req.body.password},function(err, response){
data += response;
if(data.length > 0){
var newUser = {id: req.body.id, password: req.body.password};
req.session.users = newUser.id;
console.log((req.session.users));
res.redirect('/');
}else{
var newUser = {id: req.body.id, password: req.body.password};
req.session.users = newUser.id;
console.log((req.session.users));
var newuser = new users({
id: req.body.id,
password: req.body.password });
newuser.save(function(err, Users){
if(err)
res.send(err);
else
res.send("success/n login agian");
});
}
});
}
});
app.post('/',function(req,res){
var personInfo = req.body; //Get the parsed information
if(!personInfo.name || !personInfo.age || !personInfo.number ){
res.render('show_message', {message: "Sorry, you provided worng info", type: "error"});
} else {
var newPerson = new detailsdata({
name: personInfo.name,
age: personInfo.age,
number : personInfo.number,
date : personInfo.date,
});
newPerson.save(function(err, detailsdata){
if(err)
res.render('show_message', {message: "Database error", type: "error"});
else
res.render('show_message', {message: "New person added", type: "success", person: personInfo});
});
}
});
app.post('/uploadfile', upload.single('image'), function (req, res)
{
var orginalname = req.file.orginalname;
var filename = req.file.filename;
var path = req.file.path;
var destination = req.file.destination;
var size = req.body.size;
var personInfo = req.body; //Get the parsed information
if(!personInfo.name || !personInfo.age || !personInfo.number ){
res.render('show_message', {message: "Sorry, you provided worng info", type: "error"});
} else {
var newPerson = new detailsdata({
name: personInfo.name,
age: personInfo.age,
number : personInfo.number,
image : path,
date : personInfo.date
});
newPerson.save(function(err, detailsdata){
if(err)
res.render('show_message', {message: "Database error", type: "error"});
else
res.redirect("/");
});
}
});
app.get("/api/session/",function(req,res){
users.find({"id":req.session.users}, function(err, response){
var json=JSON.stringify(response);
var data = JSON.parse(json);
res.json(data);
});
});
app.get("/api",function(req,res){
detailsdata.find(
function(err, response){
var json=JSON.stringify(response);
var data = JSON.parse(json);
res.json(data);
});
}) ;
app.get("/edit/:number",function(req,res){
res.sendFile(__dirname+"/edit.html");
});
app.post("/edit/:number",function(req,res,err){
res.sendFile(__dirname+"/personaldetails.htm");
});
app.get("/api/edit/:id",function(req,res){
detailsdata.find({"_id":req.params.id} ,
function(err, response){
var json=JSON.stringify(response);
var data = JSON.parse(json);
res.json(data);
});
});
app.post("/api/editSubmit",upload.single('image'),function(req,res){
console.log(req.file.path);
var detailsdata = mongoose.model("person", dataSchema);
var newPerson = new detailsdata({
name: req.body.name,
age: req.body.age,
number : req.body.number,
date : req.body.date,
});
detailsdata.findByIdAndUpdate(req.body.id,{"name" : req.body.name ,"age" : req.body.age ,"number" : req.body.number ,"image" : req.file.path ,"date": req.body.date }, function(err, detailsdata){
console.log(err);
if(err)
res.render('show_message', {message: "Database error", type: "error"});
else
res.redirect("/personaldetails");
});
});
app.get("/delete/:number",function(req,res){
detailsdata.findByIdAndRemove(req.params.number, function(err, detailsdata){
if(err)
res.render('show_message', {message: "Database error", type: "error"});
else
res.redirect("/personaldetails");
});
});
app.get("/personaldetails",function(req,res){
res.sendFile(__dirname+"/personaldetails.htm");
}) ;
var server = app.listen(8521, function () {
var host = server.address().address
var port = server.address().port
console.log("Server Address ::" +host);
console.log("Example app listening at http://" +host);
})