-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
337 lines (288 loc) · 9.84 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
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
327
328
329
330
331
332
333
334
335
336
337
console.log('Server-side code running');
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const client = require('socket.io').listen(4000).sockets;
const clientfeedback = require('socket.io').listen(4040).sockets;
const clientfeedback1=require('socket.io').listen(4080).sockets;
const app = express();
const {exec}=require('child_process');
var cron = require('node-cron');
var nodemailer = require('nodemailer');
// serve files from the public directory
app.use(express.static('public'));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// connect to the db and start the express server
let db;
// ***Replace the URL below with the URL for your database***
const url = 'mongodb://localhost:27017/blind';
// E.g. for option 2) above this will be:
// const url = 'mongodb://localhost:21017/databaseName';
MongoClient.connect(url, (err, database) => {
if (err) {
return console.log(err);
}
cron.schedule('*/0.5 * * * *',()=>{
console.log('running a task every two minutes');
exec('mk.bat',(err,stdout,stderr)=>{
if(err)
{
console.error(err);
return;
}
else
{
console.log(stdout);
}
})
})
db = database;
app.listen(8080, () => {
console.log('listening to 8080');
})
clientfeedback.on('connection', function (socket) {
console.log('Socket connected');
let donate = db.collection('donate');
// create function to send status//whenever we want to side something from servere side to client side we use emit to do so to show it in html file
sendStatus = function (s) {
socket.emit('status', s);
}
//get chats from mongo collection
donate.find().limit(100).sort({_id:1}).toArray(function (err, res) {
if (err) {
throw err;
}
// emit the messages
socket.emit('output', res);
});
// handle input events
socket.on('input',function(data){
let username= data.username;
let ngoname = data.ngoname;
// check for name and message
if(username=='' || ngoname=='')
{
sendStatus('pls enter an address and status');
}
else{
donate.insert({username:username,ngoname:ngoname},function(){
client.emit('output',[data]);
//send status object
sendStatus({
message:'Message sent',
clear:true
});
});
}
});
// handle clear
socket.on('clear',function(data){
//remove all chats from the collection
street.remove({},function(){
socket.emit('cleared');
});
});
});
clientfeedback1.on('connection', function (socket) {
console.log('Socket connected');
let feedback = db.collection('feedback');
// create function to send status//whenever we want to side something from servere side to client side we use emit to do so to show it in html file
sendStatus = function (s) {
socket.emit('status', s);
}
//get chats from mongo collection
feedback.find().limit(100).sort({ _id: 1 }).toArray(function (err, res) {
if (err) {
throw err;
}
// emit the messages
socket.emit('output', res);
});
// handle input events
socket.on('input', function (data) {
let name = data.name;
let message = data.message;
// check for name and message
if (name == '' || message == '') {
sendStatus('pls enter an address and status');
}
else {
feedback.insert({ name: name, message: message }, function () {
client.emit('output', [data]);
//send status object
sendStatus({
message: 'Message sent',
clear: true
});
});
}
});
// handle clear
socket.on('clear', function (data) {
//remove all chats from the collection
street.remove({}, function () {
socket.emit('cleared');
});
});
});
client.on('connection', function (socket) {
console.log('Socket connected');
let feedback = db.collection('feedback');
// create function to send status//whenever we want to side something from servere side to client side we use emit to do so to show it in html file
sendStatus = function (s) {
socket.emit('status', s);
}
//get chats from mongo collection
feedback.find().limit(100).sort({ _id: 1 }).toArray(function (err, res) {
if (err) {
throw err;
}
// emit the messages
socket.emit('output', res);
});
// handle input events
socket.on('input', function (data) {
let name = data.name;
let message = data.message;
// check for name and message
if (name == '' || message == '') {
sendStatus('pls enter an address and status');
}
else {
feedback.insert({ name: name, message: message }, function () {
client.emit('output', [data]);
//send status object
sendStatus({
message: 'Message sent',
clear: true
});
});
}
});
// handle clear
socket.on('clear', function (data) {
//remove all chats from the collection
street.remove({}, function () {
socket.emit('cleared');
});
});
});
console.log('Loading completed');
});
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'manmeetkaur18102000@gmail.com',
pass: 'Gigobibo1#1'
},
tls: {
rejectUnauthorized: true
}
});
// serve the homepage
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/signup', (req, res) => {
res.sendFile(__dirname + '/public/signup.html');
});
app.get('/signin', (req, res) => {
res.sendFile(__dirname + '/public/signin.html');
});
app.get('/feedback', (req, res) => {
res.sendFile(__dirname + '/public/feedback.html');
});
app.get('/feedback1', (req, res) => {
res.sendFile(__dirname + '/public/feedback1.html');
});
app.get('/donate', (req, res) => {
res.sendFile(__dirname + '/public/donate.html');
});
app.get('/helpers', (req, res) => {
res.sendFile(__dirname + '/public/helpers.html');
});
app.get('/contactus', (req, res) => {
res.sendFile(__dirname + '/public/contact-us.html');
});
// add a document to the DB collection recording the click event
app.post('/register', (req, res) => {
console.log(req.body);
var signin = req.body;
db.collection('signin').find().sort({ userid: -1 } ).limit(1).toArray(function (err, resLoc) {
if (resLoc.length > 0) {
signin.userid = parseInt(resLoc[0].userid) + 1;
}
else {
signin.userid = 1;
}
db.collection('signin').save(signin, (err, result) => {
if (err) {
return console.log(err);
}
let HelperOptions = {
from: '"Manmeet kaur"<manmeetkaur18102000@gmail.com>',
to: signin.useremail,
subject: "your user id for using our portal to locate consultant is" + signin.userid + " Use this to login in the future",
text: 'wow',
};
transporter.sendMail(HelperOptions, (error, info) => {
if (error) {
console.log(error);
}
console.log("message sent");
});
console.log('click added to db');
res.status(200).send(result);
});
});
});
app.post('/donateinfo', (req, res) => {
console.log(req.body);
var donate = req.body;
db.collection('donate').save(donate, (err, result) => {
if (err) {
return console.log(err);
}
console.log('click added to db');
res.status(200).send(result);
});
});
app.post('/send', (req, res) => {
console.log(req.body);
var feedback = req.body;
db.collection('feedback').save(feedback, (err, result) => {
if (err) {
return console.log(err);
}
console.log('click added to db');
res.status(200).send(result);
});
});
app.get('/map', (req, res) => {
console.log(req.query);
db.collection('signin').find({ "userid": parseInt(req.query.userid), "userPassword": req.query.userpassword }).limit(100).sort({ _id: 1 }).toArray(function (err, resUser) {
if (err) {
throw err;
}
if (resUser.length > 0) {
db.collection('location').find({ "userid": parseInt(req.query.userid) }).limit(100).sort({ _id: 1 }).toArray(function (err, resLoc) {
if (err) {
throw err;
}
// emit the messages
res.json({
'location': resLoc
});
});
}
else {
// emit the messages
res.json({
'location': resUser
});
}
});
});