-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.js
128 lines (128 loc) · 3.44 KB
/
server1.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
let express = require('express');
let app = express();
let fs = require('fs');
const mysql=require('mysql');
const port=8080;
const db = mysql.createConnection(
{
user:"root",
host: "127.0.0.1",
password: "",
database: "csau",
port: 3306,
}
)
db.connect((err)=>{
if(err)
{
throw err;
}
console.log("MySQL Connected");
})
app.get('/maxAge', function(req, res){
fs.readFile(__dirname + "/" + "backend_task1.json", 'utf8', function(err, data){
data = JSON.parse(data)
let result1=[],max=Number.MIN_VALUE;
for (let value of data.people)
{
if(max<value.age)
{
max=value.age;
}
}
for (let value of data.people)
{
if(max===value.age)
{
result1.push(value);
}
}
res.send(result1);
});
})
app.get('/phoneMatch', function(req, res){
fs.readFile(__dirname + "/" + "backend_task1.json", 'utf8', function(err, data){
data = JSON.parse(data);
let result2=[];
for (let value of data.people)
{
if(value.number[0]===value.number[9])
{
result2.push(value);
}
}
res.send(result2);
});
})
app.post('/membershipRegistration', function(req,res){
const name=req.body.name;
const rgno=req.body.rgno;
const dept=req.body.dept;
const tag=req.body.tag;
const domain=req.body.domain;
const phno=req.body.phno;
const email=req.body.email;
let resulterror=["","","",""];
if(name.length>30)
{
resulterror[0]="Length of name is larger 30";
}
else if(name.length===0)
{
resulterror[0]="Length of name should not be 0";
}
if(rgno<1000000000||rgno>9999999999)
{
resulterror[1]="Registration number should be length of 10";
}
if(phno<1000000000||phno>9999999999)
{
resulterror[2]="Phone number should be length of 10";
}
if(!email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/))
{
resulterror[3]="Invalid email address";
}
if(resulterror[0].length>=1||resulterror[1].length>=1||resulterror[2].length>=1||resulterror[3].length>=1)
{
res.send(resulterror);
}
else
{
db.query("SELECT * FROM REGISTRATION WHERE EMAIL=?", [email],
(err,result)=>{
if(err)
{
console.log(err);
res.send("Unsuccessful");
}
else
{
if(result.length==1)
{
res.send("Already Registred with this email");
}
else
{
db.query("INSERT INTO REGISTRATION VALUES (?,?,?,?,?,?,?)",
[email,name,rgno,dept,tag,domain,phno],
(err,result)=>{
if(err)
{
console.log(err);
res.send("Unsuccessful");
}
else{
res.send("Sucesss");
}
});
}
}
})
}
})
var server = app.listen(port, function(){
var host = server.address().address
var port = server.address().port
console.log("REST API demo app listening at http://%s:%s", host, port)
})