-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (88 loc) · 2.61 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
const express = require('express');
const app = express();
require('dotenv').config();
const { v4 } = require('uuid');
const Joi = require('joi');
const logger = require('./logger');
const morgan = require('morgan');
const helmet = require('helmet');
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(express.static('public'));
app.use(morgan('tiny'));
app.use(helmet());
const users = [];
const minName = 3;
// User larni response qilib yuborish
app.get('/users', (req, res) => {
res.status(200).send(users);
});
// Yangi User qo'shish
app.post('/users', (req, res) => {
const { error, value } = validateUser(req.body);
if (error) {
return res.status(400).send(error.details[0].message);
}
const user = {
id: v4(),
name: req.body.name,
stack: req.body.stack
}
users.push(user);
res.status(201).send(user);
});
// User ma'lumotlarini yangilash
app.put('/users/:id', (req, res) => {
// User-ni id orqali topish
const user = users.find(u => u.id === req.params.id);
// User topilmasa 404 error qaytarish
if (!user) {
return res.status(404).send('User topilmadi');
}
// User topilsa so'rovni validatsiya qilish
const { error, value } = validateUser(req.body);
// User validatsiyadan o'tmasa, 400 bad req qaytarish
if (error) {
res.status(400).send(error.details[0].message);
}
// User-ni yangilash
user.name = req.body.name;
user.stack = req.body.stack;
// Yangilangan User-ni qaytarish(res)
res.status(200).send(user);
})
// User-ni o'chirish
app.delete('/users/:id', (req, res) => {
// User-ni id orqali topish
const user = users.find(u => u.id === req.params.id);
// User topilmasa 404 error qaytarish
if (!user) {
return res.status(404).send('User topilmadi :(');
}
// O'chirish kerak bo'lgan user-ni aniqlash;
const userIndex = users.indexOf(user);
// User-ni o'chirish;
users.splice(userIndex, 1);
// O'chirilgan user-ni ko'rsatish
res.status(200).send(user);
})
// User-ni ID orqali topish
app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === req.params.id);
if (!user) {
return res.status(404).send('User topilmadi')
}
res.status(200).send(user);
});
// User-ni validate qilish funksiyasi
function validateUser(user) {
const userSchema = Joi.object({
name: Joi.string().min(minName).required(),
stack: Joi.string().required()
});
return userSchema.validate(user);
}
const PORT = process.env.PORT || 9000;
app.listen(PORT, () => console.log(`Server ${PORT}`))