Learning Nodejs First Day: node. js Introduction

Source: Internet
Author: User

node. JS predecessor C program ape Ryan Dahl (http://four.livejournal.com/) project, according to Google's famous open source JavaScript engine V8 for two times development Web I/O Server (HTTP/ nodejs.org/). V8 is a fast JavaScript engine. Processing JS execution is very high speed.

Related tests show that FireFox, Opera and IE JS engine speed is less than V8 come fast. And, you can say. NodeJs can benefit from a single JS engine battle between browsers. 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. The Req and res in anonymous parameters represent the request object and the Response object, respectively. NodeJS writes the logical statement in a function. Description Create a server Createserver () connection This step 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 the event. The essence is also an asynchronous process. is not synchronized.


Look at the above presentation at a macro level, but it's a much lower operation. are low-level controls that are insufficient 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 test:
var sys = require ("sys "), Kiwi = require (" Kiwi "), Express = Kiwi.require (' Express '); Get ('/', function () {this.redirect ('/hello/world ')}); Get ('/hello/world ', function () {return ' Hello World '}); Get ('/goodbye/world ', function () {return ' Goodbye World '}); Run (); The URL that
Express executes and port is http://localhost:3000. The default request rules are transferred to the/hello/world folder, and the string "Hello World" is returned. 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.

webserver event programming meaning
We know and it's clear that JavaScript is single-threaded (in terms of the current popular JS v1.5), how to avoid clogging the I/O Channel (block) ?

--Event-driven (event-based)-based programming or, alternatively, a feasible way to solve thread/blockage for many background applications. Since Viusal Basic has become popular, the event-driven programming model has been familiar to us over the GUI for a very long time. If we talk about early development, then we often do one thing. is 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, which is also a naïve "event" model.

We know. JavaScript is naturally function first level, and a function can also be fed into a function's parameters (that is, to pass in a closure closure), in other words, JS is inherently and event-driven programming coincides with each other.

In view of this, node. JS's event programming philosophy is indeed a bright spot.

On the other hand, high-concurrency Web server has been a hot spot and topic, perhaps a lot of solutions. Performance to achieve a certain index is also no problem. Just the problem is, thus. To ensure and meet the performance standards of the premise. But very rare a natural, suitable for writing business logic of the development platform. The JavaScript-based NodeJS is exactly what it needs to be, a DSL that fits most people's needs (the number of JS players is expected to be impressive ...). )。

For example, a simple counter design, I log in to a URL. It is natural to trigger a counter event.

Of course, more importantly, the language features of JS. That is, function, closure, similar C syntax, refining and concise style, and so on, especially function, simply for the event model, easily coincide with the concept 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 special site for node. js. 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/

Execute executable file (compile) directly in win:

http://www.grati.org/?

page_id=213 good node. JS China Resources

Learning Nodejs First Day: 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.