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

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.

主站蜘蛛池模板: 加查县| 平和县| 平武县| 万安县| 汶上县| 通许县| 绥江县| 乐山市| 宁波市| 长治市| 调兵山市| 保山市| 美姑县| 田阳县| 会同县| 大荔县| 靖西县| 托克逊县| 百色市| 永吉县| 上饶县| 平顺县| 肥西县| 凤翔县| 青冈县| 上思县| 贡嘎县| 洛浦县| 乌拉特前旗| 昌宁县| 宜章县| 彝良县| 边坝县| 福海县| 开原市| 分宜县| 从化市| 柳江县| 荔波县| 汨罗市| 宁安市|