James Ko
James Ko

Reputation: 34539

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. 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? Will a crude +res.status.slice(0, 3) do?


EDIT: Link to the library.

Upvotes: 0

Views: 977

Answers (1)

Evert
Evert

Reputation: 99609

All HTTP status codes have 3 digits, so your slice example will work. However, I would personally just split on space:

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

Upvotes: 1

Related Questions