国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end JS Tutorial Node.js generates HttpStatusCode auxiliary class and publishes it to npm_Basic knowledge

Node.js generates HttpStatusCode auxiliary class and publishes it to npm_Basic knowledge

May 16, 2016 pm 05:37 PM
node.js

As a good Restfull API, it not only depends on the semantics, readability, idempotence, and orthogonality of the service url, but also the http status code. A good Http Status Code gives the user a good response, such as 200 means normal success, 201 means creation success, 409 conflict, 404 resource does not exist, etc. So when I was making a demo based on node.js mongodb angularjs, I found that node.js express did not provide corresponding auxiliary classes, but I didn’t like to flood things like 201,404 with no language-level semantics everywhere, so I finally decided to write one myself. , but at the same time I am also very lazy and don’t like to do repetitive hard work. What should I do? Then copy it from the HttpStatusCode enumeration in C# that I am most familiar with. Finally, for simplicity on mac, I used node.js to parse msdn’s document about httpstatuscode to generate the node.js auxiliary class.

The code is very simple:

Copy code The code is 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);

};




});

})();

The code is hosted on github: https://github.com/greengerong/node-httpstatuscode

The final generated class is:

Copy code The code is 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 many lazy people like me, I shared this code and published it to npm. You only need npm install httpstatuscode to make it simple and practical. The following is a test demo:

Copy code The code is 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 people’s articles always have code with too much text. I hope the code can explain everything. Thank you for reading.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

See all articles