20210915 - 两种情况两种状态码
2021-09-15 00:00:00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
app.get('/api/notes/:id', (request, response) => {
Note.findById(request.params.id)
.then(note => {
if (note) {
response.json(note)
} else {
**response.status(404).end()**
}
})
.catch(error => {
console.log(error)
**response.status(400).send({error: "malformed id"})**
})
})

Untitled

Untitled

Untitled

之所以分两种状态码,是因为有两种错误。。

下面这个应该是属于invalid id吧,猜测判断是否valid是根据id的长度来的,如果长度匹配就报404,长度不匹配报400

关于400状态码

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.