Skip to content

Commit 81aa484

Browse files
added error handling middleware and user PUT req
1 parent 3be0c73 commit 81aa484

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

index.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,52 @@ require('dotenv').config()
44
const User = require('./models/user')
55
const app = express()
66

7+
const requestLogger = (request, response, next) => {
8+
console.log('---------');
9+
console.log('Method:', request.method)
10+
console.log('Path: ', request.path)
11+
console.log('Body: ', request.body)
12+
console.log('---------')
13+
next()
14+
}
15+
716
app.use(cors())
17+
app.use(express.static('build'))
818
app.use(express.json())
19+
app.use(requestLogger)
920

10-
app.get('/', (request, response) => {
11-
response.send("Hello there")
12-
})
21+
// app.get('/', (request, response) => {
22+
// response.send("Hello there")
23+
// })
1324

1425
app.get('/api/users', (request, response) => {
1526
User.find({}).then(users => {
1627
response.json(users)
1728
})
1829
})
1930

20-
app.get('/api/users/:id', (request, response) => {
31+
app.get('/api/users/:id', (request, response, next) => {
2132
const id = request.params.id
22-
console.log(id)
2333
User.findById(id)
2434
.then(user => {
2535
user
2636
? response.json(user)
2737
: response.status(404).end()
28-
}).catch(error => {
29-
response.status(400).send({error:'malformated id'}).end()
30-
})
38+
}).catch(error => next(error))
3139
})
3240

33-
app.delete('/api/users/:id', (request, response) => {
41+
app.delete('/api/users/:id', (request, response, next) => {
3442
const id = request.params.id
3543
User.findByIdAndRemove(id)
3644
.then(user => {
37-
user
38-
? response.json(user)
39-
: response.status(404).end()
40-
}).catch(error =>{
41-
console.log(error);
42-
response.status(400).send({error:'malformated id'}).end()
43-
})
45+
response.status(204).end()
46+
}).catch(error => next(error))
4447
})
4548

4649
app.post('/api/users/', (request, response) => {
4750
const user = request.body
4851

4952
const newUser = new User({
50-
"email": user.email,
5153
"fname": user.fname,
5254
"lname": user.lname,
5355
"password": user.password
@@ -62,12 +64,40 @@ app.post('/api/users/', (request, response) => {
6264
})
6365
})
6466

67+
app.put('/api/users/:id', (request, response, next) => {
68+
const user = request.body
69+
70+
const updatedUserData = {
71+
"fname": user.fname,
72+
"lname": user.lname,
73+
"password": user.password
74+
}
75+
76+
// bydefault updatedUser returns old res, optional param "{new : true}" fixes it, returns updated data
77+
User.findByIdAndUpdate(request.params.id, updatedUserData, {new : true})
78+
.then(updatedUser => {
79+
response.json(updatedUser)
80+
}).catch(error => next(error))
81+
})
82+
6583
const unknownEndpoint = (request, response) => {
6684
response.status(404).send({ error: 'unknown endpoint' })
6785
}
6886

6987
app.use(unknownEndpoint)
7088

89+
const errorHandler = (error, request, response, next) => {
90+
console.error(error.message)
91+
92+
if (error.name === 'CastError') {
93+
return response.status(400).send({ error: 'malformatted id' })
94+
}
95+
96+
next(error)
97+
}
98+
99+
app.use(errorHandler)
100+
71101
const PORT = process.env.PORT || 3001
72102
app.listen(PORT, () => {
73103
console.log(`Server running on port http://localhost:${PORT}`)

requests/addUser.rest

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ Content-Type: application/json
77
"fname": "de",
88
"lname": "delete",
99
"password": "delete"
10-
}
10+
}
11+
12+
###
13+
14+
# Update user
15+
PUT http://localhost:3001/api/users/6075bffd0a9ce8277c1e9f69
16+
Content-Type: application/json
17+
18+
{
19+
"fname": "a",
20+
"lname": "b",
21+
"password": "password"
22+
}

requests/deleteUser.rest

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DELETE http://localhost:3001/api/users/23452
1+
DELETE http://localhost:3001/api/users/6075bf35d385591fb0a1acb4

requests/getUsers.rest

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ GET http://localhost:3001/api/users/
44
###
55

66
# Get single user
7-
GET http://localhost:3001/api/users/234
7+
GET http://localhost:3001/api/users/6075bf35d385591fb0a1acb4

0 commit comments

Comments
 (0)