First day of study: node. js Introduction)

Source: Internet
Author: User

Http://blog.csdn.net/zhangxin09/archive/2010/08/25/5836777.aspx

Node. JS is senior C programmer Ryan
Dahl (http://four.livejournal.com/) works, based on Google's famous open source JavaScript Engine V8 for Secondary Development
Web
I/O Server (http://nodejs.org /). V8 itself is a very fast JavaScript Engine that processes JS execution at a very high speed. Test Table
The JS engines of Firefox, opera, and IE are slower than those of V8 idea. Moreover, it can be said that as long as the JS engine competition between browsers is not diminished for one day, nodejs can
Benefits. Make progress only when there is competition :).

The basic usage of nodejs is also very simple and clear. Let's take a look at this sentence, which is the simplest code:

View plaincopy to clipboardprint?
  1. VaR
  2. Sys = require ('sys ')
  3. , HTTP = require ('http ');
  4. HTTP. createserver (function (req, Res ){
  5. Res. writehead (200, {'content-type': 'text/plain '});
  6. Res. Write ('Hello World ');
  7. Res. Close ();
  8. }). Listen (8006 );
  9. SYS. Puts ('the server running at http: // 127.0.0.1: 8006 /');

VaR <br/> sys = require ('sys ') <br/>, HTTP = require ('http'); <br/> HTTP. createserver (function (req, Res) {<br/> res. writehead (200, {'content-type': 'text/plain '}); <br/> res. write ('Hello World'); <br/> res. close (); <br/> }). listen (8006); <br/> sys. puts ('server running at http: // 127.0.0.1: 8006 /');
The preceding statement var HTTP = require ('http'); http. createserver (function (req, Res)
{...}); An HTTP server is created to listen to requests from the client. the req and res in the anonymous parameter represent the request object and response object respectively. Nodejs writes the logic statement in
The function indicates that the createserver () connection process is asynchronous! Besides, req also has related asynchronous operations: View plaincopy to clipboardprint?

  1. HTTP. createserver (function (req, Res ){
  2. Req. addlistener ("end", function (){
  3. SYS. Puts ("request end ");
  4. });
  5. Res. writehead (200, {'content-type': 'text/plain '});
  6. Res. Write ('Hello World ');
  7. Res. Close ();
  8. }). Listen (8006 );

HTTP. createserver (function (req, Res) {<br/> req. addlistener ("end", function () {<br/> sys. puts ("request end"); <br/>}); <br/> res. writehead (200, {'content-type': 'text/plain '}); <br/> res. write ('Hello World'); <br/> res. close (); <br/> }). listen (0, 8006 );
In this way, the end event is registered for the request req. Add "request end" by using the SYS. Puts method at the end of the request ". In the process of using events, it is actually an asynchronous process, not synchronous.

From a macro perspective, the above demonstration is only a relatively low-level operation, which is low-level control, not enough to provide more high
Level Logic. To enhance the functions of the node. js platform, we can use other development modules around node. JS, or even a large-scale "framework". One of them is
Express (http://github.com/visionmedia/express ). To install Express, you must install another dependency.
Pack: kiwi (http://github.com/visionmedia/kiwi), then type kiwi-V install
To officially install Express. After installing Express, enter the following code to test:

View plaincopy to clipboardprint?
  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 ();

VaR sys = require ("sys"), <br/> kiwi = require ("Kiwi"), <br/> express = kiwi. require ('express '); </P> <p> get ('/', function () {<br/> This. redirect ('/Hello/World') <br/>}); </P> <p> get ('/Hello/world', function () {<br/> return 'Hello world' <br/>}); </P> <p> get ('/Goodbye/world', function () {<br/> return 'Goodbye world' <br/>}); </P> <p> Run ();
The URL and port used by express are http: // localhost: 3000. The default request rules will be transferred to the/Hello/World Directory and the returned
The string of 'Hello world. Access the/Goodbye/World Directory and return 'Goodbye
World '. Express is called a "framework" and should include other features, such as other advanced features of Request Routing and rendering views.

The significance of Web Server event Programming

We know and know very well that JavaScript is single-threaded (as far as JavaScript is currently popular)
V1.5), how to avoid blocking on the I/O channel? -- Event-based programming, or a feasible method, is a lot of background processes.
Solve thread/congestion in sequence. Since Viusal
Since the popularity of basic, the event-driven programming model on the GUI has long been familiar to us and has been around for a long time. If we talk about early development, one of our common practices at that time is
The main () method writes an infinite loop (while (true)
Loop), to obtain the ability to control programs at any time, is also a simple "Event" model. Javascript is actually function first.
Level, a function can also be fed into a function parameter (that is, a closure is passed in). In other words, JS naturally matches with event-driven programming and complements each other. In view
Therefore, the event programming concept of node. JS is indeed a highlight.

In addition, highly concurrent Web servers have always been a hot topic and topic for a long time. There may be many solutions, and there is no problem with performance reaching certain indicators, but the problem is, in the guaranteed and full
It is rare that a development platform that is natural and suitable for compiling business logic meets the high performance standards. nodejs, which uses JavaScript as the development language, meets this requirement.
DSL for human needs (we can estimate the number of JS players ......). For example, in a simple counter design, When I log on to a URL, a counter event is triggered, which is quite natural. Of course, more importantly
Yes, or the language features of JS, that is, function, closure, similar C syntax, refinement and concise style, etc. In particular, function is born for the event model and is easy to match.
The concept of node. js asynchronous mechanism,
It is the reason why it really attracts players to enjoy the game.

Although nodejs is still very young and has not been well developed for a long time, it is gratifying that a large number of plug-ins and enhancement packages around nodejs have emerged, some are linked databases, and some are used
Logging, template templates, and unit testing, some of which still serve
Polling ...... And so on. We can refer to nodejs specific module list in http://wiki.github.com/ry/node
/Modules.

Sharing a special node. js website is suitable for beginners:

Http://howtonode.org/

Nodejs and websocket resources:

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 node. js plug-in (c ++)

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

Run node. js on the win platform, with the help of virtual machines:

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

Run the following executable files (Compiled) in win ):

Http://www.grati.org /? Page_id = 213
Good node. js Chinese Resources

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.