NodeJS 导出和导入模块

在我之前的帖子中,我们讨论了[如何安装Enide Studio 2014 IDE](/community/tutorials/node-js-basic-examples-with-node-cli-and-node-ideNode JS Basic Examples With Node REPL (CLI) 和 Node IDE)以及**如何创建一个Node JS 应用程序**

  • 如何导出 Node JS 模块
  • 如何导入 Node JS 模块

我们将特别在下一篇文章中使用这些知识,但在我即将发布的所有示例中也将使用这些知识。

导出 Node JS 模块

首先,我们需要理解的是,为什么我们需要输出一个节点JS模块? 节点联署材料提供了几乎所有所需的模块 (我们可以在其官方网站https://www.npmjs.com/上查看此更新. 它还被称为Node JS模块存储库。 但在某些实时应用中,我们可能有一些应用功能,该应用中很多地方被使用,但在Node JS模块存储库中并不存在. 在这种情况下,为了获得Reusubility的好处,我们需要创建我们自己的新的节点JS模块. 仅仅创建新的模块并不足以在系统的其他模块中使用. 我们需要导出这个模块,以便其他模块能够重用它. 如果您是用户界面或Java开发者, 那么您是熟悉的 。 如果我们在基于Java的应用程序中有任何共同的或可重复使用的组件,那么我们把它发展成一个分离的项目,创建一个Jar文件,并在所需的项目类路径上添加. 不要太担心此时如何创建节点JS模块。 我们将讨论如何在未来的岗位上创建我们自己的新的节点JS模块。 节点JS平台提供了一种技术,用于输出以下内容,以便其他模块无需重新定义即可重新使用.

  • 变量
  • 函数
  • 模块

To export all these three, we have to use same technique. Node JS has provided an Object "exports" to do this. Syntax: NodeJS-export-module-1 We will see one by one now with some examples. Here we are discussing them theoretically only. We will take one realtime scenario and implement one Node JS Application in my next post. How to export a JavaScript Variable We use Node JS "exports" object to export a Node JS Module’s Variable so that other Modules can reuse it. In realtime, we don’t just export a Variable. However just for practice purpose and also to make it clear to beginners, we are discussing this separately. Syntax: NodeJS-export-module-2 Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: pi.js file with the following content.

1var PI = 3.1416
2exports.PI = PI;

Here we have exported PI variable as exports.PI with name PI. That’s means other Node JS project can use this variable very easily just using PI name. How to export a JavaScript Function: We use Node JS same "exports" object even to export a Node JS Module’s JavaScript function so that other Modules can reuse it. In realtime, we may do this but it is not recommended to use always. Syntax: NodeJS-export-module-3 Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

 1function add(a,b){
 2return a + b;
 3}
 4
 5function sub(a,b){
 6return a - b;
 7}
 8
 9function mul(a,b){
10return a * b;
11}
12
13function div(a,b){
14return a / b;
15}
16
17exports.add = add
18exports.sub = sub
19exports.mul = mul
20exports.div = div

Here we have exported all 4 JavaScript functions separately. That’s means other Node JS project can reuse them directly. It is a simple Arithmetic application. However if we take some realtime Node JS Applications, we can observe plenty of JavaScript files and each file may contain plenty of functions. In this scenario, it is very tedious process to export each and every function separately. To resolve this issue, we need to use Exporting a Node JS module technique as described below. How to export a Node JS Module: We use Node JS same "exports" object even to export a Node JS Module so that other Modules can reuse it. In realtime, it is recommended. Syntax: NodeJS-export-module-4 Example: Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

 1exports.arthmetic = {
 2
 3var PI = 3.1416;
 4function add(a,b){
 5return a + b;
 6}
 7
 8function sub(a,b){
 9return a - b;
10}
11
12function mul(a,b){
13return a * b;
14}
15
16function div(a,b){
17return a / b;
18}
19}

在这里,我们已经导出了所有4个JavaScript函数和PI变量,只有一个导出陈述,这意味着其他Node JS项目可以很容易地重复使用所有函数和PI。

导入 Node JS 模块

If we observe a Node JS Application or Module, it may dependent on other existing Node JS modules or our own Modules. The major advantage of Modularity is reusability. We don’t need to redevelop the same existing functionality. We can import a Module and reuse that module functionality very easily. How to import a Node JS Module: We use same technique to import our modules or existing Node JS Modules. Node JS Platform has provided a function call "require()" to import one module into another. Syntax: NodeJS-import-module-1 Here module-name is our required Node JS module name. some-name is reference name to that module. This require() call import the specified module and cached into the application so that we don’t need to import it again and again. Example:

  • 要导入我们自己的 Node JS 模块
1var arthmetic = require("arthmetic");
  • 要导入现有的 Node JS 模块导入 Node JS "express" 模块;
1var arthmetic = require("express");
2````Import Node JS "mongoose" 模块;```
3var mongoose = require("mongoose");
4``
5
6这个 require() 调用类似于 Java 中的 **`import`** 语句,我们使用 import 语句将包、类、接口等导入到另一个类或接口中,现在我们对如何导出和导入 Node JS 模块有了一些知识,我们将在下一篇文章中使用这些知识来创建自己的 Node JS 模块。
Published At
Categories with 技术
Tagged with
comments powered by Disqus