-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (177 loc) · 6.75 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
const express = require('express')
const mysql = require('mysql')
const app = express()
const bodyParser = require('body-parser')
// const cors = require('cors')
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
require('dotenv').config()
// app.use(cors())
const connection = mysql.createConnection(process.env.DATABASE_URL)
function fetchData(sql,fields){
return new Promise((resolve,reject)=>{
connection.query(sql, (error, results, fields) => {
if (error) {
reject(error.message);
}
console.log("Fetching the result")
console.log(results)
// resp.send(results)
resolve(results)
});
})
}
function insertData(sql,fields){
return new Promise((resolve,reject)=>{
connection.query(sql,[fields], (error, results, field) => {
if (error) {
reject(error.message);
}
console.log("Inserting the result")
console.log(results)
// resp.send(results)
resolve(results)
});
})
}
function updateData(sql,fields){
return new Promise((resolve,reject)=>{
console.log("Updating query")
console.log(fields)
connection.query(sql,fields, (error, results, field) => {
if (error) {
console.log(error)
reject(error.message);
}
console.log("Updating the result")
console.log(results)
// resp.send(results)
resolve(results)
});
})
}
app.options('*',(req,resp)=>{
resp.setHeader('Access-Control-Allow-Origin', '*');
resp.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
resp.setHeader('Access-Control-Allow-Headers','Origin, Content-Type, X-Auth-Token');
resp.setHeader('Access-Control-Allow-Headers','*');
resp.setHeader('Access-Control-Max-Age', 2592000); // 30 days
resp.writeHead(204);
resp.end();
})
app.get('/',async(req,resp)=>{
resp.send("Hello");
})
app.post('/update',async(req,resp)=>{
let errors = validate(req)
if(errors && errors.length==0){
let name = req.body.name
console.log("Check1")
console.log(req.body)
if(name!=undefined && name!=null){
let score = Number(req.body.reactjs) + Number(req.body.nodejs)
let stmt = `update try1 set name=?,email=?,phone=?,qualification=?,currentstatus=?,expectedsalary=?,nodejs=?,reactjs=?,totalscore=? where id =? `;
let fields = [
name,req.body.email,req.body.phone,req.body.qualification,req.body.currentstatus,req.body.expectedsalary,
req.body.nodejs,req.body.reactjs,score,req.body.id,
];
await updateData(stmt,fields)
}
}
let data = {}
data.errors = errors
resp.setHeader('Access-Control-Allow-Origin', '*');
resp.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
resp.setHeader('Access-Control-Allow-Headers','Origin, Content-Type, X-Auth-Token');
resp.setHeader('Access-Control-Max-Age', 2592000); // 30 days
if(errors && errors.length>0){
resp.writeHead(400);
} else {
resp.writeHead(200);
}
resp.end(JSON.stringify(data, null));
})
app.post('/insert',async(req,resp)=>{
let name = req.body.name
/**
* name:name, email:email,phone:phone,qualification:qualification,currentstatus:currentStatus,expectedsalary:expectedSalary,
nodejs:nodeJs,reactjs:reactJs,totalscore
*/
let errors = validate(req)
if(errors && errors.length==0){
if(name!=undefined && name!=null){
let score = Number(req.body.reactjs) + Number(req.body.nodejs)
let stmt = `INSERT INTO try1(name,email,phone,qualification,currentstatus,expectedsalary,nodejs,reactjs,totalscore) VALUES ?`;
let fields = [
[name,req.body.email,req.body.phone,req.body.qualification,req.body.currentstatus,req.body.expectedsalary,
req.body.nodejs,req.body.reactjs,score]
];
// let stmt = `INSERT INTO try1(name,email) VALUES ?`;
// let fields = [
// [name,name]
// ];
await insertData(stmt,fields)
}
//
} else {
//
}
let data = {}
data.errors = errors
resp.setHeader("Content-Type", "application/json");
resp.setHeader('Access-Control-Allow-Origin', '*');
resp.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
resp.setHeader('Access-Control-Allow-Headers','Origin, Content-Type, X-Auth-Token');
resp.setHeader('Access-Control-Max-Age', 2592000); // 30 days
if(errors && errors.length>0){
resp.writeHead(400);
} else {
resp.writeHead(200);
}
resp.end(JSON.stringify(data, null));
})
function validate(req){
//check for mandatory values
let errors = []
console.log(req.body)
if(req.body.name!=undefined && req.body.email!=undefined && req.body.phone!=undefined && req.body.qualification!=undefined
&& req.body.currentstatus!=undefined && req.body.expectedsalary!=undefined &&
req.body.nodejs!=undefined && req.body.reactjs!=undefined){
} else {
errors.push("Mandatory fields are required")
}
if(req.body.name!=undefined && req.body.name.length>255){
errors.push("Maximum limit of name is 255 characters")
}
if(req.body.email!=undefined && req.body.email.length>255){
errors.push("Maximum limit of email is 255 characters")
}
let pattern = /^\+[1-9]{1}[0-9]{3,14}$/
if(req.body.phone!=undefined && (req.body.phone.length>15 || req.body.phone.match( pattern ))){
errors.push("Phone no should be of proper format")
}
if(req.body.expectedsalary!=undefined && !parseInt(req.body.expectedsalary) && req.body.expectedsalary!=0){
errors.push("Expected Salary should be of proper format")
}
return errors
}
app.post('/fetch',async (req,resp)=>{
resp.setHeader('Access-Control-Allow-Origin', '*');
resp.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
resp.setHeader('Access-Control-Allow-Headers','Origin, Content-Type, X-Auth-Token');
resp.setHeader('Access-Control-Max-Age', 2592000); // 30 days
// await connection.connect(function(err) {
// if (err) throw err;
// console.log("Connected!");
// })
let sql = `SELECT * FROM try1`;
let result = await fetchData(sql)
resp.setHeader("Content-Type", "application/json");
resp.writeHead(200);
resp.end(JSON.stringify(result, null));
//resp.send("<html>hello</html>")
})
app.listen("3001",()=>{
console.log("Server started")
})