官术网_书友最值得收藏!

The basics of Node.js

With the basics of JavaScript out of the way, let's focus on some of the basics of Node.

Event-driven

At its core, one of the most powerful features of Node is that it is event-driven. This means that almost all the code you write in Node is going to be written in a way that is either responding to an event or is itself firing an event (which in turn will fire other code listening for that event).

Let's take a look at the code that we'll write in a later chapter that handles connecting to a MongoDB server using Mongoose, a popular Node.js MongoDB Object Document Mapper (ODM) module:

mongoose.connect(');
mongoose.connection.on('open', function() {
console.log("Connected to Mongoose...");
});

First, we tell our mongoose object to connect to the server provided as a string parameter to the function. Connecting will take an undetermined amount of time though, and we have no way of knowing how long. So, what we do is bind a listener to the open event on the mongoose.connection object. With the use of the on keyword, we are indicating that when the mongoose.connection object triggers an open event, it executes the anonymous function that was passed in as the parameter.

Asynchronous execution

Earlier, we reviewed the idea of asynchronous JavaScript code in the browser using setTimeout—the principles apply more strongly in the world of Node. As you may be making a number of network-dependent connections to different REST API services, database servers, and anything else, it is important that your code can execute smoothly and has proper callback usage in place whenever each service responds.

The module system

In an effort to make the code as modular and reusable as possible, Node uses a module system that allows you to better organize your code. The basic premise is that you write a code fulfilling a single concern, and export this code as a module that serves that single purpose. Then, whenever you need to use that code elsewhere in your code base, you will require that module:

// ** file: dowork.js
module.exports = {
  doWork: function(param1, param2) {
    return param1 + param2;
  }  
}

// ** file: testing.js
var worker = require('./dowork'); // note: no .js in the file

var something = 1;
var somethingElse = 2;

var newVal = worker.doWork(something, somethingElse);
console.log(newVal);
// => 3

Using this system, it is simple to reuse the functionality in a module (in this case, the dowork module) in a number of other files. Furthermore, the individual files of a module act as a private namespace. Any variables declared and used within the module file are private to that module and not exposed to any code that uses the module via require().

This system extends infinitely as well. Within your modules, you can require other modules and so on and so forth.

The Node.js core

The Node.js core literally has hundreds of modules available for you to use while writing your applications. These include the following:

  • Events
  • Filesystems
  • HTTP
  • Net
  • Streams
  • Timers

    Tip

    Definitely make sure to check out the online docs on Node (at http://nodejs.org/api) to see the full list of modules available in Node's core and see plenty of sample code and explanations.

主站蜘蛛池模板: 盘山县| 清苑县| 海兴县| 德令哈市| 新乡市| 晴隆县| 元朗区| 江山市| 泌阳县| 什邡市| 会理县| 屏东县| 松桃| 凌海市| 赣榆县| 霍山县| 宁远县| 巫溪县| 克东县| 凌云县| 阿拉善右旗| 临泉县| 安溪县| 林州市| 双城市| 巴林右旗| 武川县| 天柱县| 吉安市| 洪洞县| 宣汉县| 嘉禾县| 林口县| 柳林县| 青川县| 长治县| 湛江市| 元朗区| 南城县| 且末县| 延吉市|