What is Node. js? (1)

Source: Internet
Author: User

Node is not a panacea! But it can solve some key problems.

Learning Node is not an easy task, but the reward you receive is worth your efforts. This is because JavaScript is the only solution to the many difficulties in Web application development.

Directory

"Are you cool? Let me use it !"Node. js provides a series of cool APIs and toolboxes for the latest programming languages, which can be applied directly to traditional Rails, Ajax, Hadoop, or even to some extent for iPhone development and HTML5.If you have attended some major technical conferences, You will always hear some keynote speeches on Node. js, even though these topics are difficult for common developers.

You may have heard that Node. js (sometimes referred to as "Node") is a server-side solution that can run JavaScript and process HTTP requests as a Web service. If these things won't make you dizzy, the discussion about ports, sockets, and threads in a twinkling of an eye will become the hottest topic at the moment, and you will find these things dazzled. Does this content really fall into the scope of JavaScript? Why do so many people in the world prefer to run JavaScript out of the browser, not to mention running JavaScript on the server?

The good news is that everything you hear about Node is correct. Node is indeed a part of network programming and is used to process server requests and responses. The bad news is that, like Rails, Ajax, and Hadoop, there are too few practical technical materials. After the Node-based "excellent" framework is mature, the technical materials will be able to keep up with each other. But why should we try to use Node again after the technical books and tutorials are released? Using Node now may bring unexpected changes to your code and even make your program easier to implement.

Expert door warning!

Like most technologies, Node is also a new bottled old wine: It looks opaque and weird, but it is favored by small development teams. If you have never touched Node, you need to learn some server scripts that are easy to use. You need time to figure out the Node, because it is very different from the client JavaScript even if it is running on the server. Actually, you have to brainwash yourself to learn about the JavaScript event processing mechanism, asynchronous IO, and some basic network knowledge.

Unfortunately, this means that if you have been using Node for development for more than two years, you will feel that the content of this article is monotonous and too simple. You will start looking for new "stimuli", such as running the Node on the client, or starting to try the event I/O, reflector mode, and npm. You will find that the world of Node is so interesting, and even many advanced Node technologies have an epic aesthetic, and these things are still difficult for beginners. Therefore, you may want to share your knowledge with your peers, especially those who do not know Node. When they are interested in Node, share with them the advanced Node technology.

Node: a few small examples

First, you should realize that Node is used to run independent JavaScript programs, rather than running in an HTML clip in the browser. It is a real file stored in the file system. It is executed by the Node program and runs in a daemon mode. It also enables listening on some ports.

Skip hello world

The most classic example is of course "Hello World", with source code on the Node official website (http://nodejs.org/docs/latest. Almost everyone starts to touch Node from Hello World. Now let's skip this simplest example and look at some more interesting examples: implement a program that can send files from the server to the client (not just send a piece of text to the client ).

 
 
  1. var sys = require("sys"),  
  2. http = require("http"),  
  3. url = require("url"),  
  4. path = require("path"),  
  5. fs = require("fs");  
  6. http.createServer(function(request, response) {  
  7.     var uri = url.parse(request.url).pathname;  
  8.     var filename = path.join(process.cwd(), uri);  
  9.     path.exists(filename, function(exists) {  
  10.         if(!exists) {  
  11.             response.writeHead(404, {"Content-Type": "text/plain"});  
  12.             response.end("404 Not Found\n");  
  13.             return;  
  14.         }  
  15.         fs.readFile(filename, "binary", function(err, file) {  
  16.             if(err) {  
  17.                 response.writeHead(500, {"Content-Type": "text/plain"});  
  18.                 response.end(err + "\n");  
  19.                 return;  
  20.             }  
  21.             response.writeHead(200);  
  22.             response.end(file, "binary");  
  23.         });  
  24.     });  
  25. }).listen(8080);  
  26. console.log("Server running at http://localhost:8080/"); 

Thanks to Mike Amundsen for providing similar implementations of this Code. This example is a piece of code submitted by Devon Govett on Nettuts +. Although it has been updated based on the new version of Node, the entire post of Devon is a very good entry-level learning material, this is especially true for beginners.

If you are a newbie, you can save the above Code to a text file and name it NodeFileServer. js. Before running the Node, you need a Node running environment. The latest Node version can be downloaded from the official website or the source code can be obtained from github. You need to compile the source code. If you are not familiar with Unix, make, and configure, you need to consult the online Compilation Manual for help.


Related Article

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.