First day of Learning Nodejs: node. js Introduction

Source: Internet
Author: User

node. JS is the work of the veteran C program ape Ryan Dahl (http://four.livejournal.com/), based on Google's famous open source JavaScript engine V8, to develop WEB I/O server two times (http ://nodejs.org/). The V8 itself is a very fast JavaScript engine that handles JS execution at a high speed. Related tests show that FireFox, Opera and IE JS engine speed is less than V8 come fast. And, it can be said that only the browser between the JS engine battle for a day without reducing, NodeJs can benefit from it. There is competition to make progress:).

NodeJS Basic use method is also very simple and clear, we have a look at this sentence, is the simplest code:
var sys = require (' sys '), HTTP = require (' http '), Http.createserver (function (req, res) {Res.writehead ($, {' C Ontent-type ': ' Text/plain '}); Res.write (' Hello world '); Res.close ();}). Listen (8006); 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 that listens 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 step to create the server Createserver () is asynchronous! In addition, Req also has related asynchronous operations:
Http.createserver (function (req, res) {Req.addlistener ("End", function () {sys.puts ("request End"); }); Res.writehead ($, {' Content-type ': ' Text/plain '}); Res.write (' Hello world '); Res.close ();}). Listen (8006);

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


Look at the above demonstration from the macro level, above the above but a lower operation, are low level control, not enough to provide many other high level logic. To enhance the platform functionality of NodeJS, we are able to develop modules using other surround NodeJS, even a "frame" to take shape-one of which is Express (http://github.com/visionmedia/express). The prerequisite for installing Express is to install a dependency package: Kiwi (Http://github.com/visionmedia/kiwi), and then type Kiwi-v install express enough to formally Mount Express. After installing Express, immediately enter the following code to test:
<textarea cols="50" rows="15" name="code" class="javascript">var sys = require ("sys"), Kiwi = require ("Kiwi"), Express = Kiwi.require (' Express '); get ('/', function () {This.redir ECT ('/hello/world ')}); Get ('/hello/world ', function () {return ' Hello World '}); Get ('/goodbye/world ', function () {return ' Goodbye World '}); Run ();</textarea>
Express executes the URL and port is http://localhost:3000, the default request rules will be transferred to the/hello/world folder, return the ' Hello World ' string. Visit the/goodbye/world folder and return to ' Goodbye World '. Express is called a "framework", there should be other functions, such as request routing, rendering view of other advanced features.

The significance of webserver event programming
We know and it's clear that JavaScript is single-threaded (in the case of the current popular JS v1.5), how to avoid clogging the I/O Channel (block)?—— event-driven (event-based)-based programming or maybe a viable way, For many background programs to solve the thread/blockage of the use. Since Viusal Basic has become popular, the event-driven programming model has been familiar to us over the GUI for a very 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 the program at any point in time, and to be considered a simple "event" model. We know that JavaScript is naturally the function first level, and to a function can also feed a function of the parameters (that is, to pass in a closure closure), in other words, JS natural and event-driven programming coincides with each other. In view of this, node. JS's event programming philosophy is indeed a bright spot.

Another point, the high concurrency of the WEB server has been a well-developed hot spots and topics, there may be a lot of solutions, performance to achieve a certain index is no problem, but the problem is, so, in order to ensure and meet performance standards, but very rare a natural, suitable for writing business logic development platform to JavaScript is a NodeJS for developing languages, a DSL that meets the needs of most people (the number of JS players is expected to be impressive ...). )。 For example, a simple counter design, I login to a URL, it triggers 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 young and not sophisticated, it is gratifying to note that a large number of plug-ins, enhancements, and links to databases are now emerging around NodeJS, some for logging, template templates, unit tests, Some still serve the long link under the polling poll .... Wait a minute. We are able to see a detailed list of NodeJS modules in Http://wiki.github.com/ry/node/modules.

Share a node. js site for those who have just started learning:

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) executed directly in win:

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

First day of Learning Nodejs: node. js Introduction

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.