-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (49 loc) · 1.25 KB
/
Copy pathindex.js
File metadata and controls
71 lines (49 loc) · 1.25 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express')
const users = require("./MOCK_DATA.json") //databse
const app = express();
const PORT =8000;
const fs = require('fs')
//Middleware - plugin
app.use(express.urlencoded({extended:false}));
//middleware for creating a text file within some details
app.use((req,res, next) =>{
fs.appendFile(
"log.txt",
`\n${Date.now()}:${req.ip}:${req.method}:${req.path}`,
(err,data)=>{
next();
}
)
})
// Routes
app.get('/api/users' , (req,res) =>{
return res.json(users)
})
//get the user by name
app.get('/users' ,(req,res)=>{
const html =`
<ul>
${users.map((user) =>`<li>${user.first_name}</li>`)}
</ul>
`
res.send(html)
});
//get the user by id
app.get('/api/users/:id' ,(req,res)=>{
const id = Number(req.params.id);
const user = users.find((user) =>user.id===id);
return res.json(user)
})
//create new users
app.post("/api/users" , (req,res) => {
return res.json({status :"pending"})
})
//edit the user with id
app.patch("/api/users/:id" , (req,res) => {
return res.json({status :"pending"})
})
//delete the user with id
app.delete("/api/users/:id" , (req,res) => {
return res.json({status :"pending"})
})
app.listen(PORT,()=>console.log(`Server started at Port:${PORT}`))