First day of Learning Nodejs: node. js Introduction

Source: Internet
Author: User

node. JS is the work of Senior C programmer Ryan Dahl (http://four.livejournal.com/), based on Google's famous open source JavaScript engine V8, to develop a Web I/O server two times (HTTP/ nodejs.org/). The V8 itself is a very fast JavaScript engine, and processing JS execution runs very fast. Related tests show that FireFox, Opera and IE JS engine speed is less than V8 come fast. And, it can be said, as long as the browser between the JS engine War day, NodeJs can benefit from it. There is competition to make progress:).

NodeJS basic usage is also very simple and clear, let's take a look at this sentence, is the simplest code:

[JavaScript]View Plaincopy
    1. Var
    2. SYS = require (' sys ')
    3. , HTTP = require (' http ');
    4. Http.createserver (function (req, res) {
    5. Res.writehead ($, {' content-type ': ' Text/plain '});
    6. Res.write (' Hello World ');
    7. Res.close ();
    8. }). Listen (8006);
    9. Sys.puts (' Server running at http://127.0.0.1:8006/');

The above statement var http = require (' http '); Http.createserver (function (req, res) {...}); is to create an HTTP server to listen for requests from the client, and the Req and res in the anonymous parameters represent the request object and the Response object, respectively. NodeJS writes the logical statement in a function, stating that the process of creating the server Createserver () is asynchronous! In addition, Req also has related asynchronous operations:

[JavaScript]View Plaincopy
    1. Http.createserver (function (req, res) {
    2. Req.addlistener ("End",function () {
    3. Sys.puts ("request End");
    4. });
    5. Res.writehead ($, {' content-type ': ' Text/plain '});
    6. Res.write (' Hello World ');
    7. Res.close ();
    8. }). Listen (8006);

This registers the end event with the request req. At the end of the request, add "request End" by using the Sys.puts method. In the process of using events, the essence is also an asynchronous process, non-synchronous.


Looking at the above demonstration from the macro level, above is only a relatively low-order operation, which is not enough to provide more high levels of logic. To enhance the platform functionality of NodeJS, we can use other development modules around NodeJS, even a "framework" that takes shape--one of which is Express (http://github.com/visionmedia/express). The prerequisite for installing Express is to install another dependent package: Kiwi (Http://github.com/visionmedia/kiwi), and then type Kiwi-v install Express in order for the Express to be installed formally. After installing Express, immediately enter the following code to test:

[JavaScript]View Plaincopy
  1. var sys = require ("sys"),
  2. Kiwi = require ("Kiwi"),
  3. Express = Kiwi.require (' Express ');
  4. Get ('/', function () {
  5. this.redirect ('/hello/world ')
  6. });
  7. Get ('/hello/world ', function () {
  8. return ' Hello World '
  9. });
  10. Get ('/goodbye/world ', function () {
  11. return ' Goodbye World '
  12. });
  13. Run ();


Express runs the URL and port is http://localhost:3000, the default request rules will be transferred to the/hello/world directory, return ' Hello World ' string. Visit the/goodbye/world directory and Return to ' Goodbye World '. Express is called a "framework" and there should be other features, such as request routing and other advanced features of the rendered view.

The significance of the Web server's event programming
We know and it is clear that JavaScript is single-threaded (in terms of the current popular JS v1.5), how to avoid blocking the I/O Channel (block)?—— event-driven (event-based)-based programming or perhaps a viable way, For many background programs to solve the thread/blocking use. Since Viusal Basic has become popular, the event-driven programming model has been familiar to us over the GUI for a long time. When it comes to early development, it is often a practice to write an infinite loop (while (true) loop) in the main () method, to gain the ability to control any moment of the program, and also to be a simple "event" model. We know that JavaScript is naturally a function first level, and for a function can also be fed into the parameters of a parameter (that is, passing in a closure closure), in other words, JS natural and event-driven programming is consistent, complementary. In view of this, node. JS's event programming philosophy is indeed a bright spot.

Another point, high-concurrency Web server has been a well-known hot and topic, there may be a lot of solutions, performance to achieve a certain number of indicators also no problem, but the problem is, so, in order to ensure and meet performance standards, but rarely see a natural, suitable for writing business logic development platform, to JavaScript is a NodeJS for developing languages, a DSL that fits most people's needs (estimating the number of JS players can be quite impressive ...). )。 For example, a simple counter design, I log in a URL, trigger a counter event, very natural. Of course, more importantly, JS language features, namely function, closures, similar C syntax, refining and concise style, and so on, especially Function, simply for the event model, easily coincide with the idea of node. js asynchronous mechanism, is really to attract players to enjoy the reason.


Although NodeJS is still very young and not sophisticated, it is gratifying to note that there are a large number of plug-ins, enhancements, links to databases around NodeJS, some for logging, template templates, unit tests, Some still serve the long link under the polling poll .... Wait a minute. We can refer to the NodeJS specific module list in Http://wiki.github.com/ry/node/modules.

Share a node. JS feature site for beginners:

http://howtonode.org/

Resources for Nodejs and WebSocket:

Http://blog.new-bamboo.co.uk/2009/12/7/real-time-online-activity-monitor-example-with-node-js-and-websocket

Http://blog.johngoulah.com/2010/03/nodejs-websockets-and-the-twitter-gardenhose/http://blog.andregoncalves.com /2009/12/29/nodejs-twitter-streaming-with-html5-websockets.html

Write a plugin for node. JS (c + +)

https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/

Run node. JS on the win platform with virtual machines:

http://www.lazycoder.com/weblog/2010/03/18/getting-started-with-node-js-on-windows/

Executable file (compiled) running directly on win:

http://www.grati.org/?page_id=213 good node. JS Chinese Resources

First day of Learning Nodejs: node. js Introduction

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.