In-depth introduction to NodeJS

Source: Internet
Author: User

 

Environment: Win7, Node. js V0.8.9 (http://nodejs.org /)

Open the node. js homepage and click "Install". The default installation is in C: \ Program Files \ nodejs.

What is the purpose of Node?

The goal publicly stated by Node is "to provide a simple method for building scalable network programs ". What is the problem with the current server program? Let's make a math. In languages such as Java and PHP, each connection generates a new thread, and each new thread may require 2 MB of memory. In a system with 8 gb ram, theoretically the maximum number of concurrent connections is 4,000. As your customer base grows, if you want your Web applications to support more users, you must add more servers. Of course, this will increase the costs of servers, traffic, and labor. In addition to these increases in costs, there is also a potential technical problem: users may use different servers for each request. Therefore, any shared resource must be shared among all servers. For all of the above reasons, the bottleneck in the entire Web application architecture (including traffic, processor speed, and memory speed) is the maximum number of concurrent connections that the server can process.

Node can solve this problem by changing the connection method to the server. Each connection emits an event running in the Node engine process, instead of generating a new OS thread for each connection (and allocating some supporting memory ). Node claims that it will never be deadlocked because it does not allow the use of locks at all, and it will not directly block I/O calls. Node also claims that its server can support tens of thousands of concurrent connections.

Now that you have a program that can process tens of thousands of concurrent connections, what can you build with Node? If you have a Web application that needs to process so many connections, it will be a terrible thing! It is a problem of "if you have this problem, it is not a problem at all. Before answering the above questions, let's take a look at the working principle of Node and its design and running mode.

// these modules need to be imported in order to use them.// Node has several modules.  They are like any #include// or import statement in other languagesvar http = require("http");var url = require("url");// The most important line in any Node file.  This function// does the actual process of creating the server.  Technically,// Node tells the underlying operating system that whenever a// connection is made, this particular callback function should be// executed.  Since we're creating a web service with REST API,// we want an HTTP server, which requires the http variable// we created in the lines above.// Finally, you can see that the callback method receives a 'request'// and 'response' object automatically.  This should be familiar// to any PHP or Java programmer.http.createServer(function(request, response) {     // The response needs to handle all the headers, and the return codes     // These types of things are handled automatically in server programs     // like Apache and Tomcat, but Node requires everything to be done yourself     response.writeHead(200, {"Content-Type": "text/plain"});     // Here is some unique-looking code.  This is how Node retrives     // parameters passed in from client requests.  The url module     // handles all these functions.  The parse function     // deconstructs the URL, and places the query key-values in the     // query object.  We can find the value for the "number" key     // by referencing it directly - the beauty of JavaScript.     var params = url.parse(request.url, true).query;     var input = params.number;     // These are the generic JavaScript methods that will create     // our random number that gets passed back to the caller     var numInput = new Number(input);     var numOutput = new Number(Math.random() * numInput).toFixed(0);          // Write the random number to response     response.write(numOutput);          // Node requires us to explicitly end this connection.  This is because     // Node allows you to keep a connection open and pass data back and forth,     // though that advanced topic isn't discussed in this article.     response.end();   // When we create the server, we have to explicitly connect the HTTP server to   // a port.  Standard HTTP port is 80, so we'll connect it to that one.}).listen(8888);// Output a String to the console once the server starts up, letting us know everything// starts up correctlyconsole.log("Random Number Generator Running...");

Save the above Code to drive F as Random. js, and then open IE9 to access the corresponding website, as shown below:

If you change the Number =, the text on the page is randomly changed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.