时间:2021-05-26
作为一个好的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 < 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);
};
});
})();
代码寄宿在github:https://github.com/greengerong/node-httpstatuscode
最终生成类为:
复制代码 代码如下:
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
};
最后考虑到或许还有很多像我一样懒散的人,所以共享此代码发布到了npm,只需要npm install httpstatuscode,便可以简单实用,如下是一个测试demo:
复制代码 代码如下:
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");
懒人的文章总是代码多余文字,希望代码能说明一切,感谢各位能阅读。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一、安装npm镜像(1)下载node.js,配置node.js的环境变量检测PATH环境变量是否配置了Node.js,点击开始=》运行=》输入"cmd"=>输入
一、安装前说明环境依赖:node.js;vue官方脚手架:vue-cli;包管理工具:npm模块化打包工具:webpack安装node.js会默认安装npm,v
一:npm的安装由于新版的node.js已经集成了npm的环境,所以只需去官网下载node.js并安装,安装完成后使用cmd检测是否成功。测试node的版本号:
Node.js的下载node下载地址:https://nodejs.org/zh-cn/download/下载后安装即可。新版Node.js自带npm包管理器#
npm是Node.JS的程序包管理器。进行Node.JS开发时,经常使用它安装/卸载程序包。实际上,发布程序包的工作也是由它来完成的。配置package.jso