简体   繁体   English

如何在Node中解析HTTP状态代码?

[英]How to parse HTTP status codes in Node?

I'm using an API library in Node that doesn't return the status code of the HTTP response. 我在Node中使用的API库不会返回HTTP响应的状态代码。 However, it does return the response headers, which look like this: 但是,它确实返回响应标头,如下所示:

{ ...
  status: '200 OK',
  ... }

Is there any Node library I can use to parse the status code from the returned object's status? 我可以使用任何Node库从返回的对象的状态中解析状态代码吗? Will a crude +res.status.slice(0, 3) do? 粗略的+res.status.slice(0, 3)做吗?


EDIT: Link to the library. 编辑: 链接到库。

All HTTP status codes have 3 digits, so your slice example will work. 所有HTTP状态代码都有3位数字,因此您的分片示例将可用。 However, I would personally just split on space: 但是,我个人只是在空间上分开:

var code = status.split(' ')[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM