-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (53 loc) · 1.79 KB
/
app.js
File metadata and controls
72 lines (53 loc) · 1.79 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
const express = require('express');
const app = express();
const cors = require('cors');
const dotenv = require('dotenv');
dotenv.config();
const dbService = require('./dbService');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended : false }));
// create
app.post('/insert', (request, response) => {
const { name } = request.body;
const db = dbService.getDbServiceInstance();
const result = db.insertNewName(name);
result
.then(data => response.json({ data: data}))
.catch(err => console.log(err));
});
// read
app.get('/getAll', (request, response) => {
const db = dbService.getDbServiceInstance();
const result = db.getAllData();
result
.then(data => response.json({data : data}))
.catch(err => console.log(err));
})
// update
app.patch('/update', (request, response) => {
const { id, name } = request.body;
const db = dbService.getDbServiceInstance();
const result = db.updateNameById(id, name);
result
.then(data => response.json({success : data}))
.catch(err => console.log(err));
});
// delete
app.delete('/delete/:id', (request, response) => {
const { id } = request.params;
const db = dbService.getDbServiceInstance();
const result = db.deleteRowById(id);
result
.then(data => response.json({success : data}))
.catch(err => console.log(err));
});
app.get('/search/:name', (request, response) => {
const { name } = request.params;
const db = dbService.getDbServiceInstance();
const result = db.searchByName(name);
result
.then(data => response.json({data : data}))
.catch(err => console.log(err));
})
app.listen(process.env.PORT, () => console.log('app is running'));