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

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.

主站蜘蛛池模板: 德兴市| 多伦县| 诸城市| 清水县| 神木县| 沅江市| 景德镇市| 靖边县| 贡山| 淮滨县| 高密市| 大足县| 海南省| 南城县| 澄城县| 高州市| 保山市| 武城县| 重庆市| 象州县| 拉孜县| 乐平市| 龙山县| 台北县| 新野县| 应城市| 德钦县| 海林市| 大足县| 吴忠市| 海阳市| 盐津县| 宁夏| 尼玛县| 岐山县| 平邑县| 雅安市| 密云县| 黄浦区| 临澧县| 隆回县|