-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (72 loc) · 1.84 KB
/
server.js
File metadata and controls
81 lines (72 loc) · 1.84 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
const express = require('express');
const path = require('path');
const { displayTasks, addTask, editTask, deleteTask, getTaskName } = require('./database');
const app = express();
app.use(express.json());
app.use(express.static('./public'));
app.get('/home', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'home.html'));
});
app.get('/edit/:id', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'editPage.html'));
});
app.get('/tasks', (req, res) => {
displayTasks()
.then((val) => {
const [row] = val;
res.json(row);
})
.catch((err) => {
console.error(err);
res.status(500).send('Server Error');
});
});
app.delete('/tasks/:id', (req, res) => {
deleteTask(req.params.id)
.then((val) => {
console.log(val);
res.send('Deleted successfully');
})
.catch((err) => {
console.error(err);
res.status(500).send('Server Error');
});
});
app.get('/tasks/:id', (req, res) => {
getTaskName(req.params.id)
.then((val) => {
res.json(val[0][0]);
})
.catch((err) => {
console.error(err);
res.status(500).send('Server Error');
});
});
app.post('/tasks', (req, res) => {
const { taskId, taskName } = req.body;
addTask(taskId, taskName)
.then(() => {
console.log(res);
res.send('Added successfully');
})
.catch((err) => {
console.error(err);
res.status(500).send('Server Error');
});
});
app.put('/edit/:id', (req, res) => {
const { taskId, taskName } = req.body;
editTask(taskId, taskName)
.then(() => {
console.log('Edited');
res.send('Edit Success');
})
.catch((err) => {
console.error(err);
res.status(500).send('Server Error');
});
});
const PORT = 9100;
app.listen(PORT, () => {
console.log(`The Server started listening on port: ${PORT}...`);
});