What exactly is node. js?

Source: Internet
Author: User

Source: Https://www.ibm.com/developerworks/cn/opensource/os-nodejs/index.html?ca=drs#ibm-pcon What does Node aim to solve? Supports tens of thousands of concurrent connections to address a large number of user accesses, high concurrency issues Node is definitely not what? Node is a server program. However, the underlying Node product is definitely not like Apache or Tomcat. Essentially, those servers are "ready-to-install" server products that support the immediate deployment of applications. With these products, you can start and run a server within a minute. Node is definitely not the product. Similarly, with the addition of a PHP module to allow developers to create dynamic Web pages and add an SSL module for secure connections, the node also has a modular concept that allows modules to be added to the node kernel. In fact, there are hundreds of modules available for Node, and the community is very active in creating, publishing, and updating modules, and can even handle dozens of modules a day. How does node work? Node will actually use the V8 JavaScript engine written by Google and rebuild it to be available on the server. Event-driven programming modelListing 1. Event-driven programming using JQuery on the client
1 //jQuery code on the client-side showing how Event-driven programming works2  3 //When a button was pressed, an Event occurs-deal with it4 //directly right here in a anonymous function, where all the5 //necessary variables is present and can be referenced directly6$ ("#myButton"). Click (function(){7      if($ ("#myTextField"). val ()! = $ ( This). Val ())8Alert ("Field must match button text");9});
JavaScript is a great event-driven programming language because it allows anonymous functions and closures, and more importantly, anyone who writes code is familiar with its syntax. The callback function that is called when an event occurs can be written at the capture event. This makes code easy to write and maintain, with no complex object-oriented framework, no interfaces, and no over-design possibilities. Just listen to the event, write a callback function, and everything else can be handed to the system for processing!   Sample Node ApplicationCreate a very simple WEB application, an application built to achieve the fastest speed. Application Specific requirements: Create a random number generator RESTful API. This application should accept an input: A parameter named "number". The application then returns a random number between 0 and the parameter, and returns the resulting number to the caller. Because you want the application to become a widely popular application, it should be able to handle 50,000 concurrent users. Let's take a look at the following code: Listing 2. Node Random number generator
1 //These modules need to is imported in order to use them.2 //Node has several modules. They is like any #include3 //or import statement in other languages4 varHTTP = require ("http");5 varurl = require ("url");6  7 //The most important on any Node file. This function8 //does the actual process of creating the server. Technically,9 //Node tells the underlying operating system that whenever aTen //connection is made, this particular callback function should be One //executed. Since we ' re creating a Web service with REST API, A //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 ' the //and ' response ' object automatically. This should is 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 is handled automatically in server programs +      //Like Apache and Tomcat, but Node requires everything to is done yourself AResponse.writehead ($, {"Content-type": "Text/plain"}); at   -      //Here is some unique-looking code. 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 in      //by referencing it directly-the beauty of JavaScript. -      varparams = Url.parse (Request.url,true). query; to      varinput =Params.number; +   -      //these is the generic JavaScript methods that would create the      //Our random number this gets passed back to the caller *      varNuminput =NewNumber (input); $      varNumoutput =NewNumber (Math.random () * numinput). toFixed (0);Panax Notoginseng        -      //Write The random number to response the Response.Write (numoutput); +        A      //Node requires us to explicitly end this connection. this is because the      //Node allows 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 had to explicitly connect the HTTP server to -    //a port. Standard HTTP Port is a, so we'll connect it to that one. -}). Listen (80); the   - //Output a String to the console once the server starts up, letting us know everythingWuyi //starts up correctly theConsole.log ("Random number Generator Running ...");
Start the applicationPut the above code in a file named "Random.js". Now, to start the application and run it (to create an HTTP server and listen for connections on port 80), simply enter the following command at your command prompt:% node random.js. Here's what it looks like when the server is up and running: [Email protected]:/home/moila/ws/mike# node random.jsrandom number Generator Running ... Here is the code I practiced and how it worked: Accessing the applicationThe application is up and running. Node is listening to all the connections, let's test it. Since we have created a simple RESTful API, we can use a Web browser to access this application. Type the following address (make sure you have completed the steps above): Http://localhost/?number=27. Your browser window will change to a random number between 0 and 27. Click the Reload button on your browser and you'll get another random number. That's it, this is your first Node app! Access effect:

What exactly is node. js?

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.