-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
140 lines (113 loc) · 3.52 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require('express');
const db = require('./database.js');
const bodyParser = require('body-parser');
const cors = require('cors')
// Server port
const HTTP_PORT = 3000;
// Create server / express app
const app = express();
// Parse body urlencoded or as json
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// enable CORS for all routes
app.use(cors())
// Start server
app.listen(HTTP_PORT, () => {
console.log(`Server running on port ${HTTP_PORT}`);
})
// Root endpoint
app.get('/', (request, response, next) => {
response.json({ message: 'Ok' });
});
app.get('/profile', (request, response, next) => {
let sql = 'SELECT * FROM profile';
let params = [];
db.all(sql, params, (error, rows) => {
if (error) {
response.status(400).json({ error: error.message });
return;
}
response.json(rows);
});
});
app.get('/profile/:name', (request, response, next) => {
let sql = 'SELECT * FROM profile WHERE firstname = ?';
let params = [request.params.name];
db.get(sql, params, (error, row) => {
if (error) {
response.status(400).json({ error: error.message });
return;
}
response.json(row);
});
});
app.post('/profile', (request, response, next) => {
// Validation
let errors = [];
if (!request.body.firstname) {
errors.push("No first name specified");
}
if (!request.body.lastname) {
errors.push("No last name specified");
}
if (errors.length) {
response.status(400).json({ "error": errors.join(",") });
return;
}
let data = {
firstname: request.body.firstname,
lastname: request.body.lastname,
gender: request.body.gender,
bio: request.body.bio,
age: request.body.age
};
let sql = 'INSERT INTO profile (firstname, lastname, gender, bio, age) VALUES (?,?,?,?,?)';
let params = [data.firstname, data.lastname, data.gender, data.bio, data.age];
db.run(sql, params, (error, result) => {
if (error) {
response.status(400).json({ error: error.message });
return;
}
data.id = this.lastId
response.json(data);
});
});
app.patch('/profile/:id', (request, response, next) => {
let data = {
firstname: request.body.firstname,
lastname: request.body.lastname,
gender: request.body.gender,
bio: request.body.bio,
age: request.body.age
};
let sql = `UPDATE profile SET
firstname = COALESCE(?,firstname),
lastname = COALESCE(?,lastname),
gender = COALESCE(?,gender),
bio = COALESCE(?,bio),
age = COALESCE(?,age)
WHERE id = ?`;
let params = [data.firstname, data.lastname, data.gender, data.bio, data.age, request.params.id];
db.run(sql, params, (error, result) => {
if (error) {
response.status(400).json({ message: error.message });
return;
}
response.json({ "message": "updated" });
});
});
app.delete('/profile/:id', (request, response, next) => {
let sql = 'DELETE FROM profile WHERE id = ?';
db.run(sql, [request.params.id], (error, row) => {
if (error) {
response.status(400).json({ error: error.message });
return;
}
response.json({ "message": "deleted" })
});
});
// Default response for other requests - error 404
app.use((request, response) => {
response.status(404);
});
module.exports = app;