-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (71 loc) · 1.87 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
const mysql = require('mysql2')
const bodyparser = require('body-parser')
const express = require('express')
const app = express()
const port = process.env.PORT || 1122;
//mysql db credentials
let mysqlConnection = mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: 'Password@1',
database: 'crud',
}
)
//mysql db connection
mysqlConnection.connect((error) => {
if (error) {
console.log(error)
} else {
console.log('MySQL Connection Successfully')
}
})
//http request body handler(json data)
app.use(bodyparser.json())
//get api
app.get('/users', (req, res) => {
try {
let sqlQuery = "SELECT * FROM users"
mysqlConnection.query(sqlQuery, (err, result, fields) => {
res.send(result)
});
} catch (error) {
res.status(500).send(error.message)
}
})
//post api
app.post('/users', (req, res) => {
try {
let sqlQuery = `INSERT INTO users (id,name,city,age) VALUES ('${req.body.id}','${req.body.name}','${req.body.city}','${req.body.age}')`;
mysqlConnection.query(sqlQuery, (err, result, fields) => {
res.send(result)
});
} catch (error) {
res.status(500).send(error.message)
}
})
//put api
app.put('/users/:id', (req, res) => {
try {
let sqlQuery = `UPDATE users SET name='${req.body.name}', city='${req.body.city}', age='${req.body.age}' WHERE id= ${req.params.id}`
mysqlConnection.query(sqlQuery, (err, result, fields) => {
res.send(result)
});
} catch (error) {
res.status(500).send(error.message)
}
})
//delete api
app.delete('/users/:id', (req, res) => {
try {
let sqlQuery = "DELETE FROM users WHERE id=?"
mysqlConnection.query(sqlQuery, [req.params.id], (err, result, fields) => {
res.send(result)
});
} catch (error) {
res.status(500).send(error.message)
}
})
app.listen(port, () => {
console.log(`server is listening on the port ${port}`)
})