As a good Restfull API is not only in the service URL semantics, readability, power, orthogonal, as an HTTP status code is also very important, a good HTTP state code to the user a good response, such as 200 for normal success, 201 means to create a successful , 409 conflicts, 404 resources do not exist, and so on. So in doing a demo based on Node.js+mongodb+angularjs found that Node.js Express did not provide the corresponding auxiliary class, but I do not like to 201,404 of these languages without language level of the meaning of the full, So finally decided to write one, but at the same time I am also very lazy, do not like to do repetitive coolie live, how to do? Copy it from the HttpStatusCode enumeration in my most familiar C #, and finally use the Node.js to parse MSDN's HttpStatusCode document generation Node.js for simplicity on the Mac.
The code is simple:
Copy Code code as follows:
var http = require (' http ');
var fs = require (' FS ');
var $ = require (' jquery ');
var output = "Httpstatuscode/index.js";
(function () {
String.Format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length-1; i++) {
var reg = new RegExp ("\\{" + i + "\}", "GM");
s = S.replace (Reg, arguments[i + 1]);
}
return s;
};
var options = {
Host: ' msdn.microsoft.com ',
PORT:80,
Path: '/zh-cn/library/system.net.httpstatuscode.aspx '
};
Http.get (options,function (response) {
var html = "";
Response.on ("Data", function (chunk) {
HTML + chunk;
). On (' End ', function () {
Handler (HTML);
}. On (' Error ', function (e) {
Console.log ("Got error:" + e.message);
});
function Gethttpstatuscode (htmlstring) {
var $doc = $ (HTML);
var rows = $doc. Find ("Table#memberlist tr:gt (0)");
var status = {};
Rows.each (function (i,row) {
status[$ (Row). Find ("Td:eq (1)"). Text ()] =
parseint ($ (row). Find ("Td:eq (2)"). Text (). Match (/\d+/). toString ());
});
return status;
};
function Generatecode (status) {
var code = "";
Code + + "Exports.httpstatuscode =" + json.stringify (status) + ";";
return code;
};
function WriteFile (code) {
Fs.writefile (output, code, function (ERR) {
if (err) {
Console.log (ERR);
} else {
Console.log ("The file was saved" + Output + "!");
}
});
};
function handler (HTML) {
var status = Gethttpstatuscode (HTML);
var code = Generatecode (status);
WriteFile (code);
};
});
})();
Code boarding in Github:https://github.com/greengerong/node-httpstatuscode
The final generated class is:
Copy Code code as follows:
View Code
Exports.httpstatuscode = {
"Continue": 100,
"Switchingprotocols": 101,
"OK": 200,
"Created": 201,
"Accepted": 202,
"Nonauthoritativeinformation": 203,
"Nocontent": 204,
"Resetcontent": 205,
"Partialcontent": 206,
"Multiplechoices": 300,
"Ambiguous": 300,
"movedpermanently": 301,
"Moved": 301,
"Found": 302,
"Redirect": 302,
"Seeother": 303,
"Redirectmethod": 303,
"Notmodified": 304,
"UseProxy": 305,
"Unused": 306,
"Temporaryredirect": 307,
"Redirectkeepverb": 307,
"Badrequest": 400,
"Unauthorized": 401,
"Paymentrequired": 402,
"Forbidden": 403,
"NotFound": 404,
"Methodnotallowed": 405,
"Notacceptable": 406,
"Proxyauthenticationrequired": 407,
"Requesttimeout": 408,
"Conflict": 409,
"Gone": 410,
"Lengthrequired": 411,
"preconditionfailed": 412,
"Requestentitytoolarge": 413,
"Requesturitoolong": 414,
"Unsupportedmediatype": 415,
"Requestedrangenotsatisfiable": 416,
"expectationfailed": 417,
"upgraderequired": 426,
"Internalservererror": 500,
"Notimplemented": 501,
"Badgateway": 502,
"Serviceunavailable": 503,
"Gatewaytimeout": 504,
"Httpversionnotsupported": 505
};
Finally, considering that there may be a lot of lazy people like me, so share this code posted to NPM, only the NPM install HttpStatusCode, it can be simple and practical, the following is a test demo:
Copy Code code as follows:
var HttpStatusCode = require ("HttpStatusCode"). HttpStatusCode;
var tobeequal = function (actual,expected) {
if (actual!== expected) {
Throw (actual + "Not equal" + expected);
}
};
Tobeequal (httpstatuscode.ok,200);
Tobeequal (httpstatuscode.created,201);
Tobeequal (httpstatuscode.badrequest,400);
Tobeequal (httpstatuscode.internalservererror,500);
Console.log (HttpStatusCode);
Console.log ("Success");
Lazy person's article is always code redundant text, hope the code can explain everything, thank you for reading.