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 ).
- var sys = require("sys"),
- http = require("http"),
- url = require("url"),
- path = require("path"),
- fs = require("fs");
- http.createServer(function(request, response) {
- var uri = url.parse(request.url).pathname;
- var filename = path.join(process.cwd(), uri);
- path.exists(filename, function(exists) {
- if(!exists) {
- response.writeHead(404, {"Content-Type": "text/plain"});
- response.end("404 Not Found\n");
- return;
- }
- fs.readFile(filename, "binary", function(err, file) {
- if(err) {
- response.writeHead(500, {"Content-Type": "text/plain"});
- response.end(err + "\n");
- return;
- }
- response.writeHead(200);
- response.end(file, "binary");
- });
- });
- }).listen(8080);
- 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.