Nodejs Series ~ First small example, realizes the Request.QueryString function

Source: Internet
Author: User

on Baidu Encyclopedia:

node. JS is a set of JavaScript toolkits for writing high-performance Web servers, starting with a series of changes, and in node, HTTP is the top priority. node is optimized for creating HTTP servers, so most of the samples and libraries you see online are focused on the Web (HTTP framework, template Library, and so on).

Advantages of node. JSNodejs, as an emerging background language, has a lot of attractions: RESTful API single-threaded node. JS can still process tasks in parallel without adding additional external threads--node.js is single-threaded. It implements parallel operations through event polling, which we should take advantage of--avoid blocking operations as much as possible and use non-blocking operations instead. Non-blocking IO event driven below I have been written by one of the previous information, so that we have a clearer understanding of Nodejs, the previous article Node is designed to solve what problem?

Node's avowed goal is "to provide a simple way to build scalable network programs." What is wrong with the current server program? Let's do a math problem. In languages such as Java™ and PHP, each connection will generate a new thread, and each new thread may require 2 MB of companion memory. On a system with 8 GB of RAM, theoretically the maximum number of concurrent connections is 4,000 users. As your customer base grows, you must add more servers if you want your WEB application to support more users. This, of course, increases costs such as server costs, traffic costs, and labor costs. In addition to these cost increases, there is a potential technical problem that users may use different servers for each request, so any shared resources must be shared across all servers. For all these reasons, the bottleneck in the overall WEB application architecture, including traffic, processor speed, and memory speed, is the maximum number of concurrent connections that the server can handle.

Node solves this problem by changing the way it connects to the server. Each connection emits an event that runs in the Node engine's process instead of generating a new OS thread for each connection (and allocating some companion memory for it). Node claims that it will never deadlock, because it does not allow locks at all, and it does not directly block I/O calls. Node also claims that the server running it can support tens of thousands of concurrent connections.

Now that you have a program that can handle tens of thousands of concurrent connections, what can you actually build through Node? If you have a WEB application that needs to handle so many connections, it would be a "scary" thing! It's a question of "If you have this problem, then it's not a problem at all." Before answering the above questions, let's look at how Node works and how it's designed to work.

Node is definitely not what?

Yes, 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. The entire module section of Node is discussed later in this article.

How does Node work?

Node itself runs V8 JavaScript. Wait, JavaScript on the server? Yes, you did not read wrong. Server-side JavaScript may be a new concept for programmers who use JavaScript only on the client, but the concept itself is not unreachable, so why can't I use the programming language on the client computer on the server?

What is V8? The V8 JavaScript engine is the underlying JavaScript engine Google uses for its Chrome browser. Few people think about what JavaScript actually does on the client. In fact, the JavaScript engine is responsible for interpreting and executing the code. Google uses V8 to create an ultra-fast interpreter written in C + + that has another unique feature that you can download and embed in any application. The V8 JavaScript engine is not limited to running in a single browser. As a result, Node actually uses the V8 JavaScript engine written by Google and rebuilds it to be available on the server. It's perfect! Why create a new language now that you have a good solution available?

Event-driven programming

The education that many programmers receive makes them think that object-oriented programming is the perfect programming design, which makes them dismissive of other programming methods. Node uses a so-called event-driven programming model.

Listing 1. Event-driven programming using JQuery on the client
 //  jQuery code on the client-side showing how Event-driven Programming Works  //  when a button is pressed, a Event occurs-deal with it  //  directly right here in a anonymous function, where all the  //  necessary variables is present and can be referenced directly  $ ("#myButton"). Click (function   () { if  ($ ("# MyTextField "). val ()! = $ (this   "Field must match button text"  

In fact, there is no difference between the server side and the client. Yes, there is no button click action, and no action is typed into the text field, but at a higher level, the event is occurring. A connection was built, this is an event! The data is received through the connection, which is also an event! Data is stopped through the connection, this is still an event!

Why is this setting type ideal for Node? 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!

Extended ModulesNode. JS uses module modules to classify different functions to simplify application development. The modules module is somewhat like a class library in C language. Each node. JS class Library contains a very rich variety of functions, such as the HTTP module contains a lot of functions related to the HTTP function, can help developers to easily http,tcp/udp, such as the operation, but also can easily create HTTP and tcp/ UDP server. It is very convenient to use modules in a program, just as follows: here, the HTTP class library is introduced, and the HTTP class library reference is stored in the HTTP variable. At this point, node. JS will search our app for the existence of a node_modules directory and search the directory for the presence of HTTP modules. If node. js cannot find this directory, it will go to the global module cache and the user can specify the location of the module by relative or absolute path, for example: var MyModule = require ('./mymodule.js ');

The module contains a lot of functional code snippets, the code in the module is mostly private, meaning that the function methods and variables defined in the module can only be called in the same module. Of course, some methods and variables can be exposed outside the module, this time can use the export object to implement

First need to install NODEJS environment, related file download

After the download, after the installation, in the C-drive will appear Nodejs related directory

OK, let's take a look at how to implement the Request.QueryString function, that is, the function of the GET request, the relevant code is as follows:

varQS = require (' querystring ');varHTTP = require (' http ');varFormidable = require (' formidable '));varexec = require (' child_process '). exec;varFS = require (' FS ');varfilename = ' ';varurl = require ("url");functionStart (response, request) {varGetquery =Url.parse (request.url). query; varGetData = Qs.parse (getquery);//GetData DataConsole.log (getdata["Zzl"]);}

One more standard notation to QueryString string, as follows:

var http = require ("http"); var url = require ("url"); var true ). Query;console.log (Params.zzl);

Feel the second way to be clearer

Test:

http://127.0.0.1:8888?zzl=1234

The results for the console console for Nodejs are:

Nodejs Series ~ First small example, realizes the Request.QueryString function

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.