- Learning Node.js for .NET Developers
- Harry Cummings
- 473字
- 2021-07-02 16:29:06
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.
- 大學計算機基礎(第二版)
- 軟件安全技術
- Computer Vision for the Web
- Angular UI Development with PrimeNG
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- Mastering Yii
- Visual Studio 2015高級編程(第6版)
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- OpenStack Networking Essentials
- Python語言科研繪圖與學術圖表繪制從入門到精通
- 交互式程序設計(第2版)
- Laravel Design Patterns and Best Practices
- MATLAB 2020 GUI程序設計從入門到精通
- Mastering R for Quantitative Finance
- Swift編程實戰:iOS應用開發實例及完整解決方案