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

Creating modules in Node.js

We've actually already used several Node.js modules and created some of our own. Let's look again at our application from Chapter 2, Getting Started with Node.js.

The following code is from routes/index.js and routes/users.js:

module.exports = router;

The following is the code from app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

Each of our routes (index and users) is a module. They expose their functionality using the built-in module object, which is defined by Node.js as a variable scoped to each module. In the preceding example, the object provided by each of our route modules is an Express router instance. The app.js script imports these modules using the built-in require function.

Observe that app.js also imports various npm packages using require. Note that it uses file paths to reference our own modules, whereas npm modules are referenced by name.

Let's look at how Node.js modules satisfy the three aspects of JavaScript module functionality.

Declaring a module with a name and its own scope

In Node.js, each separate JavaScript file is automatically treated as a new module. Unlike scripts loaded into a web page, each file has its own scope. The name of the module is the name of the file.

Defining functionality provided by the module

Node.js provides two built-in variables for exporting functionality from a module. These are module.exports and exports. module.exports is initialized to an empty object. exports is just a reference to module.exports. It is equivalent to the following appearing before your script:

var exports = module.exports = {};

Whatever is contained in the module.exports variable at the end of your script is the exported value of your module. This will be returned whenever your module is imported elsewhere. The following are all equivalent:

module.exports.foo = 1;
module.exports.bar = 2;

module.exports = { foo: 1, bar: 2 };

exports.foo = 1;
exports.bar = 2;

Note that the following is not the same as the previous examples. It just reassigns exports, but doesn't alter module.exports at all:

exports = { foo: 1, bar: 2 };

Importing a module into another script

Node.js provides another built-in variable for importing modules. This is the require function we saw in app.js earlier in the chapter. This function is provided by Node.js and always available. It takes a single argument, which is the name or path of the module you want to import. The following excerpts from app.js demonstrate loading a third-party module by name and one of our own modules by a file path:

var express = require('express');
...
var routes = require('./routes/index');

Note that we don't need to specify the .js file extension for our own module. Node.js will automatically add this for us.

主站蜘蛛池模板: 水城县| 南城县| 华亭县| 安远县| 高密市| 盐城市| 仁寿县| 新安县| 湖北省| 桦川县| 泾川县| 东丰县| 锡林浩特市| 泰州市| 东方市| 上杭县| 读书| 石狮市| 张家港市| 信丰县| 宁强县| 修文县| 永济市| 忻城县| 大港区| 荣昌县| 定兴县| 辉县市| 罗田县| 绵竹市| 昂仁县| 天气| 明溪县| 小金县| 新乐市| 遵化市| 金昌市| 广宗县| 金华市| 盘锦市| 杭锦后旗|