-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (101 loc) · 3.99 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
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
const Joi = require('joi'); // return from this model as a class, so J is capital
const express = require('express');
const app = express();
app.use(express.json());
// CRUD operation through HTTP ===========================
// app.get()
// app.post()
// app.put()
// app.delete()
// Handling GET requests
const courses = [
{ id: 1, name: 'Course1' },
{ id: 2, name: 'Course2' },
{ id: 3, name: 'Course3' },
];
app.get('/', (req, res) => {
res.send('Hello Bangladesh!');
});
app.get('/api/courses', (req, res) => {
// res.send([1, 2, 3]);
res.send(courses);
});
// Route parameter for single course
app.get('/api/courses/:id', (req, res) => {
// res.send(req.params);
// res.send(req.params.id);
// Handling GET requests (if exists ID or not)
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send('The course id was not found');
res.send(course);
});
// Multiple route parameters
app.get('/api/posts/:year/:month/:day', (req, res) => {
res.send(req.params); // http://localhost:3000/api/posts/2020/5/15 (for example)
// Query parameter on route =========
// res.send(req.query); // http://localhost:3000/api/posts/2020/5/15?sortBy=name (for example)
});
// Handling POST Requests ====================
app.post('/api/courses', (req, res) => {
// Input Validation under Joi npm package ==============
// const shcema = {
// name: Joi.string().min(3).max(30).required()
// };
// const result = Joi.validate(req.body, shcema);
// console.log(result);
// if (result.error) {
// res.status(400).send(result.error.details[0].message); // .details[0] is for exact message to the client
// return;
// };
// Validation through validateCourse function ==========
const { error } = validateCourse(req.body); // result.error (object destructor)
if (error) return res.status(400).send(error.details[0].message); // .details[0] is for exact message to the client
const course = {
id: courses.length + 1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
// Handling PUT request / UPDATE the courses
app.put('/api/courses/:id', (req, res) => {
// Here 3 tasks ==============
// 1. Look up the course and if doesn't exist, return 404 not found
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send('The course id was not found');
// 2. Validation the course and return Bad request ===========
// const shcema = {
// name: Joi.string().min(3).max(30).required()
// };
// const result = Joi.validate(req.body, shcema);
// const result = validateCourse(req.body);
const { error } = validateCourse(req.body); // result.error (object destructor)
if (error) return res.status(400).send(error.details[0].message); // .details[0] is for exact message to the client
// 3. Update the course and return
course.name = req.body.name;
res.send(course);
});
// Handling DELETE request
app.delete('/api/courses/:id', (req, res) => {
// Here 3 tasks ==============
// 1. Look up the course and if doesn't exist, return 404 not found
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send('The course id was not found');
// 2. Delete the course
const index = courses.indexOf(course);
courses.splice(index, 1);
// 3. Return the same course
res.send(course);
});
// Create a function validateCourse for app.put/.post/.delete
function validateCourse(course) {
const shcema = {
name: Joi.string().min(3).max(30).required()
};
return Joi.validate(course, shcema);
}
// PORT environment variable
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});