Skip to content

Instantly share code, notes, and snippets.

@oxyno-zeta
Created July 17, 2016 16:09
Show Gist options
  • Save oxyno-zeta/a60aa7b0ef4a894482ef41e55681eeca to your computer and use it in GitHub Desktop.
Save oxyno-zeta/a60aa7b0ef4a894482ef41e55681eeca to your computer and use it in GitHub Desktop.
NodeJS Get all codes in objects
const http = require('http');
const _ = require('lodash');
const INFORMATIONAL = {};
const SUCCESS = {};
const REDIRECTION = {};
const CLIENT_ERROR = {};
const SERVER_ERROR = {};
_.forEach(http.STATUS_CODES, function (value, code) {
let key = value.replace(new RegExp(' ', 'g'), '_').replace(new RegExp('-', 'g'), '_').toUpperCase();
let obj = {
code: code,
reason: value
};
if (code >= 100 && code < 200) {
INFORMATIONAL[key] = obj;
}
else if (code >= 200 && code < 300) {
SUCCESS[key] = obj;
}
else if (code >= 300 && code < 400) {
REDIRECTION[key] = obj;
}
else if (code >= 400 && code < 500) {
CLIENT_ERROR[key] = obj;
}
else {
SERVER_ERROR[key] = obj;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment