I translated an article not long ago.Article:
Asp.net uses httphandler to Package Multiple CSS or JS files to accelerate page loading
The technology of packaging, compressing, and creating cache in both the browser and client is used to optimize page loading. Recently I am studying node. js. Next let's take a look at what node. js can do in this regard.
The advantage of node. JS is that network communication and I/O are not blocked. It can be seen that it is an effective solution for high concurrency requirements. In web development, many files are static files, such as CSS files and JS files. Requests to them are usually asynchronous requests sent by the browser after the page is loaded to the client. Generally, the web server can process limited concurrent requests. For large applications, there may be enough concurrent requests for dynamic pages. Why not take advantage of node. js's server advantages to reduce the burden on Web servers?
Next, I will test the feasibility of this solution.
First, we build a website (Asp.net ).
File List:
One test page |
Default. aspx |
A style sheet File |
Stylesheet.css |
A Javascript file |
JScript. js (dynamically changing node text content |
page content:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "%> </P> <p> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> </P> <p> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead runat = "server"> <br/> <title> </title> <br/> <! -- Common Request Method --> <br/> <LINK rel = "stylesheet" href = "stylesheet.css"/> <br/> <SCRIPT type = "text/JavaScript" src = "jscript. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> <Form ID =" form1 "runat =" server "> <br/> <div> <br/> <P class = "A"> This is a test page. </P> <br/> <P class = "B" id = "HW"> Hello world! </P> <br/> <input type = "button" id = "testbtn" onclick = "test. change (); "value =" Change innertext "/> <br/> </div> <br/> </form> <br/> </body> <br/> </ptml> <br/>
The above is our usual practice. These requests are usually taken over by the Web server first and then processed further (not discussed here ).
Running effect:
CSS effect:
JS effect. After clicking the button:
Click the button to switch the content of the second p tag (this mainly indicates that the JS file is valid and accessible)
However, node. in JS, we can leave the requests for these static files to the Web server for processing, and let them be handed over to another node. JS-created HTTP server for processing (by requesting the port that the HTTP server is listening ).
Use the node. js file request method:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "%> </P> <p> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> </P> <p> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead runat = "server"> <br/> <title> </title> <br/> <! -- Common Request Method --> <br/> <% -- <LINK rel = "stylesheet" href = "stylesheet.css"/> <br/> <SCRIPT type = "text/JavaScript "src =" jscript. JS "> </SCRIPT> -- %> </P> <p> <! -- Node. JS Request Method --> <br/> <LINK rel = "stylesheet" href = "http: // localhost: 8080/stylesheet.css "/> <br/> <SCRIPT type =" text/JavaScript "src =" http: // localhost: 8080/JScript. JS "> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <Form ID =" form1 "runat =" Server "> <br/> <div> <br/> <P class =" A "> This is a test page. </P> <br/> <P class = "B" id = "HW"> Hello world! </P> <br/> <input type = "button" id = "testbtn" onclick = "test. change (); "value =" Change innertext "/> <br/> </div> <br/> </form> <br/> </body> <br/> </ptml> <br/>
Of course, this is only the Request Method of the client. It is not enough to have these methods. Next we need to build the processing logic of the server:
(1) create a test. js file under the cygwin root directory;
(2) type the following JS:
VaR sys = require ("sys"), <br/> HTTP = require ("HTTP"), <br/> url = require ("url "), <br/> Path = require ("path"), <br/> FS = require ("FS"); <br/> HTTP. createserver (function (request, response) {<br/> var uri = URL. parse (request. URL ). pathname; <br/> var filename = path. join (_ dirname, Uri); <br/> path. exists (filename, function (exists) {<br/> If (! Exists) {<br/> response. writehead (404, {"Content-Type": "text/plain"}); <br/> response. write ("404 Not found \ n"); <br/> response. end (); <br/> return; <br/>}</P> <p> FS. readfile (filename, "binary", function (ERR, file) {<br/> If (ERR) {<br/> response. writehead (500, {"Content-Type": "text/plain"}); <br/> response. write (ERR + "\ n"); <br/> response. end (); <br/> return; <br/>}</P> <p> response. writehead (200); <br/> response. write (file, "binary"); <br/> response. end (); <br/> }). listen (8080); </P> <p> sys. puts ("server running at http: // localhost: 8080 /");
(3) Run cygwin and enter the command: node/test. js.
You can see that the HTTP server has been built:
Next, let's let it process client requests. It searches for the file in the file system and responds to the client according to the combined path of the requested file.
Page effect:
We can see that the effect is the same as that of the common practice.
Obviously, node. js can easily accomplish these tasks. I/O and communication without blocking are its advantages. In addition, it can read files asynchronously. Asynchronous and event-based drivers have always been the advantages of Js.
This example only lists the files such as JS and CSS. In fact, all static files can be switched to this request method. Of course, this example cannot be used as a Deployment Solution for commercial applications. This is only a feasibility test, but it is obvious that it is indeed a solution, but there are still many problems to consider. For example, build cache to improve efficiency, File compression, encryption, file structure access in distributed architecture, node. JS server security issues and so on.
But as a rising technology, I believe it will be improved slowly.
Recently, I am learning about node. js. You can talk more about node. js ~
Mailto: yanghua1127@gmail.com