-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (129 loc) · 4.04 KB
/
app.js
File metadata and controls
153 lines (129 loc) · 4.04 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
const express = require('express')
const bodyparser= require('body-parser')
const mysql = require('mysql2')
const app= express()
const port = process.env.PORT || 5000
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
//Connect database
const pool= mysql.createPool({
connectionLimit :10,
host :'localhost',
user :'root',
password :'12345678',
database :'ss_pclub'
});
//getData
app.get('',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Select * from student_data', (err,rows) => {
connection.release()
if(!err) {
res.send(rows)
}
else {console.log(err)}
})
})
})
app.get('/Branch/:dat',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Select * from student_data where Branch = ?',[req.params.dat], (err,rows) => {
connection.release()
if(!err) {
res.send(rows)
}
else {console.log(err)}
})
})
})
app.get('/Name/:dat',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Select * from student_data where Name = ?',[req.params.dat], (err,rows) => {
connection.release()
if(!err) {
res.send(rows)
}
else {console.log(err)}
})
})
})
app.get('/UserID/:dat',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Select * from student_data where UserID = ?',[req.params.dat], (err,rows) => {
connection.release()
if(!err) {
res.send(rows)
}
else {console.log(err)}
})
})
})
app.get('/RollNo/:dat',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Select * from student_data where RollNo = ?',[req.params.dat], (err,rows) => {
connection.release()
if(!err) {
res.send(rows)
}
else {console.log(err)}
})
})
})
//Deleting a Record
app.delete('/:dat',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
connection.query('Delete from student_data where RollNo = ?',[req.params.dat], (err,rows) => {
connection.release()
if(!err) {
res.send('Record Deleted')
}
else {console.log(err)}
})
})
})
//Adding a record
app.post('',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
const params= req.body
connection.query('Insert into student_data Set ?',params, (err,rows) => {
connection.release()
if(!err) {
res.send('Record Added')
}
else {console.log(err)}
})
console.log(req.body)
})
})
//Update
app.put('',(req,res) =>{
pool.getConnection((err,connection) => {
if(err) throw err
console.log('Connected as id' ,connection.threadId)
const params= req.body
const {UserID,Name,Branch,RollNo}= req.body
connection.query('Update student_data Set Name = ?, Branch= ? where RollNo = ? ',[Name,Branch,RollNo], (err,rows) => {
connection.release()
if(!err) {
res.send('Record Updated')
}
else {console.log(err)}
})
})
})
//Listen to port
app.listen(port, () => console.log('Listen on Port:',port))