-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·333 lines (260 loc) · 11.3 KB
/
server.js
File metadata and controls
executable file
·333 lines (260 loc) · 11.3 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var express = require('express');
var app = express();
var db=require('./db');
var fs = require('fs');
var multer = require('multer');
var app=express();
var upload = multer({ dest: './uploads/'});
var ImageName;
var nodemailer=require('nodemailer');
var WebSocketServer = require('websocket').server;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public_files'));
app.use(express.static('uploads'));
//the files are uploaded at file directory uploads
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
var newname =filename+Date.now();
return newname;
},
onFileUploadStart: function (file) {
// console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.name+" is uploaded successful");
ImageName=file.name;
}
}));
//now is able to handle exception
app.post('/api/photo',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded");
});
});
//get book photos
app.get('/getImages/',function(req,res){
// var imageName=req.params[0];
// console.log("ImageName :"+ImageName);
var data;
data=ImageName;
res.send(data);
});
app.get('/',function(req,res){
res.sendfile("index.html");
});
app.post('/users',function(req,res){
var postBody=req.body;
var myUsername=postBody.username;
// console.log(myUsername);
if(!myUsername){
res.send('ERROR');
return;
}
function getQuery(username, callback){
db.query('SELECT username FROM users WHERE username=?', username,function(err,results,rows){
if(err){
callback(err,null);}
else{
callback(null,results[0]);}
});
}
var existUsername=getQuery(myUsername,function(err,data){
if(err){
throw err;}
else{
// console.log(data.username);
if(!data){
var user={username:postBody.username,password:postBody.password,firstname:postBody.firstname,lastname:postBody.lastname,address:postBody.address,university:postBody.university};
var query=db.query('INSERT INTO users SET ?', user, function(err,res){
if(err){throw err;}
console.log('Last insert ID:',res.insertId);
});
res.send('OK');
return;
}
else{
res.send('EXIST');
return;}
}
});
});
//get personal book records
app.get('/bookedit/*',function(req,res){
var nameToLookup=req.params[0];
// console.log(nameToLookup);
var query=db.query('SELECT * FROM books WHERE username=?', nameToLookup,function(err,rows){
if(err)throw err;
res.send(rows);
return;
});
});
//post book records
app.post('/bookcreate',function(req,res){
var postBody=req.body;
var myUsername=postBody.username;
if(!myUsername){
res.send('ERROR');
return;}
console.log(ImageName);
var book={username:postBody.username,bookname:postBody.bookname,ISBN:postBody.ISBN,price:postBody.bookprice,bookcondition:postBody.bookcondition,photo:ImageName,bookstatus:'To be sold'};
console.log(book)
var query=db.query('INSERT INTO books SET ?', book, function(err,res){
if(err){throw err;}
});
res.send('OK');
return;});
//get book search results
app.get('/booksearch/*',function(req,res){
var itemToLookup="%"+req.params[0]+"%";
console.log(itemToLookup);
if(isNaN(parseInt(itemToLookup))){
//title
var query=db.query('SELECT * FROM books INNER JOIN users ON books.username=users.username WHERE bookname LIKE ?', itemToLookup,function(err,rows){
if(err)throw err;
res.send(rows);
return;});
}else{
//ISBN
var query=db.query('SELECT * FROM books INNER JOIN users ON books.username=users.username WHERE ISBN LIKE ?', itemToLookup,function(err,rows){
if(err)throw err;
res.send(rows);
return;});
}
});
app.post('/users2/',function(req,res){
var postBody=req.body;
var nameToLookup=postBody.username;
var passwordToCheck=postBody.password;
console.log(nameToLookup);
console.log(passwordToCheck);
// function getQuery(nameToLookup,callback){
db.query('SELECT * FROM users WHERE username=?', nameToLookup,function(err,rows){
if(err){
throw err;
}
else{
var jsonObject=rows[0];
//check whether the username exists
if(jsonObject==null){
res.send('{}');
}
//username exists
else{
console.log(jsonObject);
console.log('password entered is '+passwordToCheck+'orignial password is '+jsonObject.password);
if(passwordToCheck===jsonObject.password)
{
res.send(jsonObject);
}
else
{
console.log('wrong password');
//send empty json object if the password is wrong
res.send('{}');
}
}
}
});
});
app.post('/bookdelete/',function(req,res){
var postBody=req.body;
var id=postBody.id;
var query=db.query('DELETE FROM books WHERE id = ?', id, function(err,res){
if(err){throw err;}
});
res.send('OK');
return;
});
app.post('/buyerconfirmation/',function(req,res){
var postBody=req.body;
var buyername=postBody.buyername;
var ownername=postBody.ownername;
var bookname=postBody.bookname;
var bookcondition=postBody.bookcondition;
var price=postBody.price;
var ISBN=postBody.ISBN;
var photo=postBody.photo;
console.log(buyername);
console.log(ownername);
console.log(bookname);
var status='book ordered';
console.log(status)
var query=db.query('UPDATE books SET bookstatus = ? WHERE bookname = ? AND username = ? AND price = ?', [status, bookname, ownername, price], function(err,res){
if(err){throw err;}
});
function getQuery(username, callback){
db.query('SELECT * FROM users WHERE username=?', buyername,function(err,results,rows){
if(err){
callback(err,null);}
else{
callback(null,results[0]);}
});
}
getQuery(buyername,function(err,data){
if(err){throw err;}
else{
console.log(data.username);
var html = '';
html+="<h>Hello,<br />";
html+="<h><br />";
html+="<h>Buyer Info:<br />";
var buyer = ['username: '+data.username,'firstname: ' +data.firstname, 'lastname: '+data.lastname, 'address: '+data.address, 'university: '+data.university];
for(var i=0; i<buyer.length;i++){
html+="<b>"+buyer[i]+"<br />";
}
html+="<h>Requested Book Info:<br />";
var bookrequested = ['bookname: '+bookname,'bookcondition: '+bookcondition,'price: '+price,'ISBN: '+ISBN];
for(var i=0; i<bookrequested.length;i++){
html+="<b>"+bookrequested[i]+"<br />";
}
//create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'booksharewebservice@gmail.com',
pass: 'recursion915'
}
});
//send email with transporter
var mail={
to: ownername,
subject: 'Book Rental Request',
html: html
};
transporter.sendMail(mail, function(err, info){
if (err) {
console.log(err);
}
res.send('OK');
return;
console.log('Message sent: ' + info.response);
});
}
}
);
});
// Websocket Reference: http://codular.com/node-web-sockets
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Server started at http://localhost:%s/', port);
});
var wsServer = new WebSocketServer({httpServer: server});
var count = 0;
var clients = {};
wsServer.on('request', function(r){
var connection = r.accept('echo-protocol', r.origin);
var id = count++;
clients[id] = connection;
console.log((new Date()) + ' Connection accepted [' + id + ']');
connection.on('message', function(message) {
var msgString = message.utf8Data;
for(var i in clients){
clients[i].sendUTF(msgString);
}
});
});