Access to http status code constants

I am looking for a list of http status codes in Javascript. Are they defined in any implementation?

I looked at XMLHttpRequest , but found only readyState constants.

 var xhr = new XMLHttpRequest(); console.log(xhr.DONE); //4 

I'm looking for something like

 console.log(xhr.statusCodes.OK); //200 

Which obviously does not exist in the xhr object.

+7
javascript
source share
2 answers

Http status codes are supported by Internet Assigned Numbers Authority (IANA), while readyState refers to XmlHttpRequest .

So just go to an authoritative source. Wikipedia article should be sufficient, as this is not a very controversial topic - or, as commented, the official list can be found here

You can also wrap the ones you are interested in in a javascript object

 var HttpCodes = { success : 200, notFound : 404 // etc } 

maybe if(response == HttpCodes.success){...}

+4
source

For node.js, you can use the node -http-status ( github ) module.

This is an example:

 var HttpStatus = require('http-status-codes'); response .status(HttpStatus.OK) .send('ok'); response .status(HttpStatus.INTERNAL_SERVER_ERROR) .send({ error: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR) }); 
+11
source

All Articles