利用Node.js為Node.js產生HttpStatusCode輔助類並發布到npm

來源:互聯網
上載者:User

作為一個好的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的輔助類。

    代碼很簡單:


  1 var http = require('http');
  2
  3 var fs = require('fs');
  4
  5 var $ = require('jquery');
  6
  7 var output = "httpStatusCode/index.js";
  8
  9 (function(){
 10
 11    
 12
 13     String.format = function() {
 14
 15         var s = arguments[0];
 16
 17         for (var i = 0; i < arguments.length - 1; i++) {
 18
 19             var reg = new RegExp("\\{" + i + "\\}", "gm");
 20
 21             s = s.replace(reg, arguments[i + 1]);
 22
 23         }
 24
 25         return s;
 26
 27     };
 28
 29
 30
 31
 32     var options = {
 33
 34         host:'msdn.microsoft.com',
 35
 36         port:80,
 37
 38         path:'/zh-cn/library/system.net.httpstatuscode.aspx'
 39
 40     };
 41
 42
 43
 44
 45     http.get(options,function (response) {
 46
 47         var html = "";
 48
 49         response.on("data",function (chunk) {
 50
 51             html += chunk;
 52
 53         }).on("end", function () {
 54
 55             handler(html);
 56
 57         }).on('error', function (e) {
 58
 59             console.log("Got error: " + e.message);
 60
 61         });
 62
 63
 64
 65
 66     function getHttpStatusCode(htmlString) {
 67
 68         var $doc = $(html);
 69
 70         var rows = $doc.find("table#memberList tr:gt(0)");
 71
 72         var status = {};
 73
 74         rows.each(function(i,row){
 75
 76             status[$(row).find("td:eq(1)").text()] =
 77
 78                 parseInt($(row).find("td:eq(2)").text().match(/\d+/).toString());
 79
 80         });
 81
 82        return status;
 83
 84     };
 85
 86     
 87
 88     function generateCode(status){
 89
 90        var code = "";
 91
 92        code += "exports.httpStatusCode = " + JSON.stringify(status) + ";";
 93
 94        return code;
 95
 96     };
 97
 98    
 99
100     function writeFile(code){
101
102         fs.writeFile(output, code, function(err) {
103
104             if(err) {
105
106                 console.log(err);
107
108             } else {
109
110                 console.log("The file was saved " + output + "!");
111
112             }
113
114         });
115
116     };
117
118
119
120
121     function handler(html){
122
123        var status = getHttpStatusCode(html);
124
125        var code = generateCode(status);
126
127        writeFile(code);
128
129     };
130
131
132
133
134   });
135
136 })();

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.