首頁 > web前端 > js教程 > 主體

Node.js產生HttpStatusCode輔助類別發佈到npm_基礎知識

WBOY
發布: 2016-05-16 17:37:52
原創
1532 人瀏覽過

作為一個好的Restfull Api不僅在於service url的語意,可讀性,冪等,正交,作為http狀態碼也很重要,一個好的Http Status Code給使用者一個很好的回應,例如200表示正常成功,201表示創建成功,409衝突,404資源不存在等等。所以在做一個基於node.js mongodb angularjs的demo時發現node.js express沒有提供相應的輔助類,但是本人不喜歡將201,404這類毫無語言層次語義的東西到處充斥著,所以最後決定自己寫一個,但是同時本人也很懶,不喜歡做重複的苦力活,怎麼辦?那就從我最熟悉的c#中HttpStatusCode枚舉中copy出來吧,最後為了簡單在mac上所以採用了利用node.js去解析msdn關於httpstatuscode的文檔生成node.js的輔助類別。

程式碼很簡單:

複製代碼 代碼如下:

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
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(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 = " J🎜>
code = "exports.httpStatusCode = " J🎜 ";";

return code;

};



function writeFile(code){

.writef , code, function(err) {

if(err) {

console.log(err);

} else {

console.log🎜>} else {

console.log ("The file was saved " output "!");

}

});
};

};

};

function handler(html){
var status = getHttpStatusCode(html);

var code = generateCode(status);

var code = generateCode(status);

writeFile(
};




});

})();



代碼寄宿在github:https://github.com/greengerong/node-httpstatuscode


最終生成類別為:






複製程式碼


程式碼如下:


View Code
exports. >"Continue": 100,
"SwitchingProtocols": 101,
"OK": 200,
"Created": 201,
"Accepted": 202, "Nonon🎜: 203,
"NoContent": 204,
"ResetContent": 205,
"PartialContent": 206,
"MultipleChoices": 300,
"Ambiguous" "MovedPermanently": 301,
"Moved": 301,
"Found": 302,
"Redirect": 302,
"SeeOther": 303,
"Redirect3" ,
"NotModified": 304,
"UseProxy": 305,
"Unused": 306,
"TemporaryRedirect": 307, "RedirectKeepVerb: 303" BadRequest": 400,

"Unauthorized": 401,

"PaymentRequired": 402,

"Forbidden": 403,

"NotFound": 404, " "NotAcceptable": 406, "ProxyAuthenticationRequired": 407, "RequestTimeout": 408, "Conflict": 409, "Gthus: 410" ": 411, "PreconditionFailed": 412, "RequestEntityTooLarge": 413,
"RequestUriTooLong": 414, "UnportedMediaType": 415" 🎜>"ExpectationFailed": 417, "UpgradeRequired": 426, "InternalServerError": 500, "NotImplemented": 501, "BadGatemented": 501, "BadGatemented" : 503, "GatewayTimeout": 504, "HttpVersionNotSupported": 505 }; 最後考慮到或許還有很多像我一樣懶散的人,所以共享此程式碼發佈到了npm,只需要npm install httpstatuscode,便可以簡單實用,如下是一個測試demo: 複製程式碼 程式碼如下:

var httpStatusCode = require("httpstatuscode").httpStatusCode;

var toBeEqual = function (actual,expected){

if(actualtion (actual,expected){

if(actual !== expected){

throw (actual " not equal " expected);

}

};

toBeEqual(httpStatusCode.OK,200); >toBeEqual(httpStatusCode.Created,201);

toBeEqual(httpStatusCode.BadRequest,400);

toBeEqual(httpStatusCode.InternalServerE>,5000); 🎜>
console.log(httpStatusCode);

console.log("success");



懶人的文章總是代碼多餘文字,希望代碼能說明一切,感謝各位能閱讀。
 
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板