怎样从Express API发送一致的错误响应

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者 | Simon Plenderleith译者 | 王强策划 | 李俊辰"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 Express 构建 API 时,可能很难知道如何发送一致的错误响应。这个框架似乎并未为此提供什么特殊功能,因此你需要自己去解决问题。某些时候,你可能会想知道自己是否在以“正确的方式”操作。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如我在《使用 Express 构建现代 API 的 5 种最佳实践》博客文章中提到的那样:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"构建 API 来发明自己的错误响应格式是非常诱人的,但是 HTTP 响应状态代码是一个很好的起点,因为它们可以传达特定的错误状态。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你发明了自己的格式,就必须在 API 中构建一堆额外的逻辑,并且你可能还要确保对它们进行了全面的测试。没有人想在错误响应代码中还看到错误,不是吗?最重要的是,它也需要客户端,例如前端 JavaScript,来实现用于处理 API 错误响应特殊格式的额外代码。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果有一种更简单的方法,一种经过实践检验的标准方法来发送错误响应,那岂不是很好吗?幸运的是,这种方法是存在的!HTTP 标准定义了状态代码,你可以在 API 响应中使用这些状态代码来指示请求是否成功,或是否发生了错误。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是一个 HTTP 错误响应示例,带有 400 状态代码,指示来自客户端的“错误请求”(Bad Request):"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"< HTTP\/1.1 400 Bad Request\n< Content-Type: text\/html; charset=utf-8\n< Content-Length: 138\n< Date: Wed, 28 Oct 2020 20:11:07 GMT\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果要发送这样的错误响应,可以使用 Express 的 res.status() 方法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"res.status(400).end();\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"遵循 HTTP 标准"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章