logo

Thông báo

Icon
Error

Chia sẻ
Tùy chọn
Xem
Xem bài viết cuối
Offline admin  
#1 Đã gửi : 17/12/2015 lúc 02:56:53(UTC)
admin

Danh hiệu: Administration

Chức danh:

Nhóm: Administrators
Gia nhập: 23-07-2013(UTC)
Bài viết: 6,117
Man
Viet Nam
Đến từ: Vietnam

Cảm ơn: 10 lần
Được cảm ơn: 2 lần trong 2 bài viết

Node.js Tutorial Series - Getting Started

Our Node.js tutorial series for beginners and experienced developers will help you start building real-time applications.

Ryan Dahl, the author of Node.js had introduced it in his talk at JSConf 2009 and he received a standing ovation from the audience for his work. Node.js is not a library. It is a platform for building server based applications using JavaScript. The official site for Node.js defines it as follows:

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Written in C++ and JavaScript, Node.js uses the V8 JavaScript engine (the JavaScript engine used by Chrome browser) for execution. Node.js installers are available for Windows, Linux and Mac.

Asynchronous Execution and Event Loop

Node.js treats I/O and network operations as slower operation and runs them asynchronously. So it doesn’t wait for the operation to complete before picking the next task in the queue. Every asynchronous operation accepts a callback function. The callback is called with two parameters once the operation is complete:

  • The first parameter contains details of error, if the execution is failed
  • The second parameter is the data returned upon successful execution of the operation

Unlike other server platforms, Node.js doesn’t have multiple threads to process the request. It has just one thread to take care of all the requests. As already stated, Node.js uses the V8 JavaScript engine for execution. Hence, the way JavaScript executes on this platform is much similar to the way it works on a browser.

Though Node.js runs on a single thread, it is capable of responding to a huge number of parallel requests. The Event loop mechanism of the platform makes this possible. The following figure illustrates how event loop works:

Event loop is also used by the browsers to handle asynchronous operations. Following listing describes the request-response cycle illustrated in the above figure:

1. Clients send request to the Node.js Server

2. The requests are either picked up by the event loop or, are asked to wait in a queue for the event loop to be free if it is currently handling another request

3. If the operation requested by the client has to be executed asynchronously, the task is delegated to worker threads and event loop picks the next task from the queue

4. If a worker thread finishes its task, it sends a notification to the event loop via a callback and it passes the result of the operation to this function

Package Management

Many programming languages and platforms have a system in place which allows them to install and manage external 3rd party modules, which extend the utility of the platform. Node.js is no different and comes with a package manager called Node Package Manager (NPM). It gets installed along with the installer of Node.js and can be used from command prompt. By default, NPM points to the global NPM repository (http://www.npmjs.org) and is able to install any package that is already available on this repository.

If you’re not sure of the name of the package you want to install, use the npm search command as follows:

Mã:
npm search <package name>

The Node.js team has built the most common packages and made them available over NPM. The community has also been actively contributing repositories to NPM. All available packages can be browsed on the official site.

Module System

Node.js shares some perceived limitations with JavaScript. One of them is there is no way to share modules between the browser, server and other frameworks. Node.js solves this problem by using the CommonJS module system. CommonJS is an open source module system designed by some developers and is used by several JavaScript based projects.

CommonJS module system is synchronous in nature. It allows modules to export data and this data is made available to the importing modules. Just like any other module system, it provides a nice way to structure code written in Node.js and helps in building apps based on large code base.

Note: Another alternative to CommonJS is RequireJS which is a JavaScript file and module loader and has been developed specifically for the browser environment.

Writing a Hello World Node.js Application

Now that you know what Node.js is and how it works, let’s write a simple Hello world application. Before you start writing the app, make sure to install Node.js by downloading the installer for your OS from the official site.

Create a new folder at your favorite location and name it HelloWorldNode. Create a new file inside the folder and change name of the file to server.js. Open this file in any text editor and type in the following code:

Mã:

var http = require('http');

http.createServer(function(request, response){

    var date = new Date();

    var dateString = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();

    response.writeHead(200, {'Content-Type': 'text/html'});

    response.end('<b>Hello World</b><br /><i>The date is: ' + dateString

+ '</i><br /><br />The requested URL is: ' + request.url);    

}).listen(3000);

This code is all that is required to start a Node.js server and start listening on a port on the system for requests. The above code listens on port 3000 and responds with the static HTML passed in the response.send method to the client browser. As the response is not specific to any URL, the same response would be sent for all requests.

The module http is installed globally and is available to any Node.js module on the system. The first statement gets a reference of the object returned by this module. The HTTP module is the low-level API to create a Node.js server.

To run this application, open a command prompt and move to the folder containing this file. Now run the following command:

Mã:
> node server.js

Start a browser and change the URL to: http://localhost:3000. The output on the page would be similar to the following:

Conclusion

You just got started with Node.js. Hope this article covers all the necessary things that one needs to know to get started with Node.js.

We will cover more features of Node.js in upcoming articles. Stay tuned!

Source : dotnetcurry.com

Ai đang xem chủ đề này?
OceanSpiders 2.0
Di chuyển  
Bạn không thể tạo chủ đề mới trong diễn đàn này.
Bạn không thể trả lời chủ đề trong diễn đàn này.
Bạn không thể xóa bài của bạn trong diễn đàn này.
Bạn không thể sửa bài của bạn trong diễn đàn này.
Bạn không thể tạo bình chọn trong diễn đàn này.
Bạn không thể bỏ phiếu bình chọn trong diễn đàn này.

| Cung cấp bởi YAF.NET 2.2.4.14 | YAF.NET © 2003-2021, Yet Another Forum.NET
Thời gian xử lý trang này hết 0.407 giây.