-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (109 loc) · 2.5 KB
/
server.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
const express = require('express');
const app = express(),
bodyParser = require("body-parser");
port = 8888;
const students = [
{
id: 1,
first_name: 'Nofar',
last_name: 'Alfasi',
address: 'Remez 20',
city: 'Rishon-Lezion',
state: 'Israel',
grade: 12
},
{
id: 2,
first_name: 'Peter',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 2
},
{
id: 3,
first_name: 'Lois',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 11
},
{
id: 4,
first_name: 'Meg',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 1
},
{
id: 5,
first_name: 'Chris',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 3
},
{
id: 6,
first_name: 'Stewie',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 12
},
{
id: 7,
first_name: 'Brian',
last_name: 'Griffin',
address: '31 Spooner Street',
city: 'Quahog',
state: 'Rhode Island',
grade: 12
}
];
app.use(bodyParser.json());
app.use(express.static(process.cwd()+"/client/dist/"));
app.get('/', (req,res) => {
console.log(`Server get on port::${port}`);
res.sendFile(process.cwd()+"/client/dist/index.html")
});
app.get('/api', (req, res) => {
console.log(`Server get on port::${port}`);
res.json(students);
});
app.get('/api/getStudents', (req, res) => {
// console.log(students);
console.log(`Server getStudents on port::${port}`);
res.json(students);
});
app.post('/api/createStudent', (req, res) => {
const student = req.body.student;
// console.log(students);
students.push(student);
console.log(`Server createStudent on port::${port}`);
// res.json("user added");
res.json(students);
});
app.post('/api/updateStudent', (req, res) => {
const student = req.body.student;
// console.log(students);
students[student.id] = student;
console.log(`Server updateStudent on port::${port}`);
// res.json("user added");
res.json(students);
});
app.delete(`/api/deleteStudent/:id`, (req, res) => {
console.log(`Server deleteStudent on the port::${port}`);
let id = req.params.id;
students.splice(id, 1);
res.json(students);
});
app.listen(port, () => {
console.log(`Server listening on the port::${port}`);
});