As a good Restfull Api, it is not only about the semantics, readability, power, and orthogonal of service URLs, but also about http Status codes. A good Http Status Code gives users a good response, for example, 200 indicates normal success, 201 indicates successful creation, 409 conflict, and 404 resource does not exist. Therefore, we are working on a node-based architecture. node is found in js + mongodb + angularjs demo. js express does not provide the corresponding auxiliary classes, but I do not like to fill the 201,404 class of things without hierarchical semantics everywhere, so I finally decided to write it myself, but I am also very lazy, what should I do if I do not like repeated hard work? Copy the code from the HttpStatusCode enumeration in c #, which I am most familiar with. In the end, node is used for mac convenience. js parses msdn documentation on httpstatuscode to generate node. js helper class.
The code is simple:
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 (codes );
128
129 };
130
131
132
133
134 });
135
136 })();