-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (43 loc) · 1.21 KB
/
index.js
File metadata and controls
51 lines (43 loc) · 1.21 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
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const port = 5000;
app.get('/', (req, res) => {
res.send('Hello from first node with nodemon');
});
const users = [
{ id: 0, name: 'Kami', email: 'kami@gmail.com', phone: '01928374658' },
{ id: 1, name: 'Sami', email: 'sami@gmail.com', phone: '01928375646' },
{ id: 2, name: 'Jami', email: 'jami@gmail.com', phone: '01928374645' }
]
app.get('/users', (req, res) => {
const search = req.query.search;
if (search) {
const searchResult = users.filter(user => user.name.toLowerCase().includes(search));
res.send(searchResult);
}
else {
res.send(users);
}
});
//app.METHOD
app.post('/users', (req, res) => {
const newUser = req.body;
newUser.id = users.length;
users.push(newUser);
console.log('hitting the post', req.body)
// res.send(JSON.stringify(newUser))
res.json(newUser)
})
//dynamic api
app.get('/users/:id', (req, res) => {
const id = req.params.id;
const user = users[id];
res.send(user);
// console.log(req.params.id);
})
app.listen(port, () => {
console.log('listening to port:', port)
});