-
Notifications
You must be signed in to change notification settings - Fork 0
/
regServer.js
executable file
·105 lines (90 loc) · 2.25 KB
/
regServer.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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var validator = require('validator');
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
app.use(bodyParser());
app.post('/reg', function(req, res){
var allGood = true;
if(!validator.isNull(req.body.email))
{
if(validator.isEmail(req.body.email))
{
//Email is valid
}
else
{
res.send(406, "Not a valid email");i
allGood = false;
}
}
else
{
res.send(406, "Email is empty");
allGood = false;
}
if(validator.isNull(req.body.name))
{
res.send(406, "Name should not be empty");
allGood = false;
}
if(!validator.isNumeric(req.body.locid))
{
res.send(406, "Location Id is invalid");
allGood = false;
}
if(allGood)
{
reg(req.body.name, req.body.email, req.body.locid, res);
}
});
app.get('/unreg/:locid/:email', function(req, res){
var allGood = true;
if(!validator.isEmail(req.params.email))
{
res.send(406,"Invalid Email");
allGood = false;
}
if(!validator.isNumeric(req.params.locid))
{
res.send(406,"Invalid Location Id");
allGood = false;
}
if(allGood)
{
unreg(req.params.email, req.params.locid, res);
}
});
//Register to mongo
function reg(name, email, locid,res)
{
MongoClient.connect('mongodb://127.0.0.1:27017/emailNot', function(err, db) {
if(err) throw err;
var collection = db.collection('emailRegs');
collection.count({'uid':email+"--"+locid}, function(err, count){
console.log(count);
if(count ==0)
{
collection.insert({'email':email, 'name':name,'locid':locid,'uid':email+"--"+locid}, function(err, docs) {
console.log("Works Fine");
res.send("Done!");
});
}
else
{res.send(406,"Item allready exist")};
});
})
}
//unreg to mongo
function unreg(email,locid,res)
{
MongoClient.connect('mongodb://127.0.0.1:27017/emailNot', function(err, db) {
if(err) throw err;
var collection = db.collection('emailRegs');
collection.remove({'uid':email+"--"+locid}, function(err, docs) {
console.log("Works Fine");
res.send("Done!");
});
})
}
app.listen(80);