nodejs导出导入

In my previous posts, we have discussed about “How to install Enide Studio 2014 IDE” and also “How to create a Node JS Application“.

在我以前的文章中,我们讨论了“ 如何安装Enide Studio 2014 IDE ”以及“ 如何创建Node JS应用程序 ”。

Before discussing about “How to create new Node JS Modules and How to reuse them in other Node JS Modules”, first of all we need to have some good knowledge about how to export and import a Node JS module.

在讨论“如何创建新的Node JS模块以及如何在其他Node JS模块中重用它们”之前,我们首先需要对如何导出导入 Node JS模块有一定的了解。

In this post, we are going to discuss the following two important Node JS concepts theoretically.

在本文中,我们将从理论上讨论以下两个重要的Node JS概念。

  • How to export a Node JS Module如何导出Node JS模块
  • How to import a Node JS Module如何导入Node JS模块

We will use this knowledge especially in next post, but also in all my coming post’s examples.

我们将在以后的文章中特别是在以后的例子中使用这些知识。

导出节点JS模块 (Export a Node JS Module)

First and foremost thing we need to understand is why we need to export a Node JS Module?

我们首先要了解的是为什么我们需要导出Node JS模块?

Node JS has provided almost all required modules (We can check this updates on its official website: https://www.npmjs.com/. It is also known as Node JS Module Repository). But in some realtime applications, we may have some application functionality, which is used many places in that application but not available at Node JS Module Repository.

Node JS提供了几乎所有必需的模块(我们可以在其官方网站上查看此更新: https : //www.npmjs.com/ ,也称为Node JS模块存储库)。 但是在某些实时应用程序中,我们可能具有某些应用程序功能,该功能在该应用程序中的许多地方都已使用,但在Node JS Module Repository中不可用。

In this scenario, to get Reusability benefit, we need to create our own new Node JS module. Just creating new Module is not enough to use it in other modules of the system. We need to export this module so that other modules will reuse it.

在这种情况下,为了获得可重用性的好处,我们需要创建自己的新Node JS模块。 仅创建新模块不足以在系统的其他模块中使用它。 我们需要导出此模块,以便其他模块可以重用它。

If you are a UI or Java Developer, then is familiar to you. If we have any common or reusable component in Java-based application, then we develop it as a separated project, create a Jar file and add it at required projects classpath.

如果您是UI或Java开发人员,那么您会很熟悉。 如果我们在基于Java的应用程序中具有任何公共或可重用的组件,则我们将其开发为一个单独的项目,创建一个Jar文件,并将其添加到所需的项目类路径中。

Don’t worry too much about how to create a Node JS Module at this point. We will discuss how to create our own new Node JS Module in future post.

此时,不必太担心如何创建Node JS模块。 我们将在以后的文章中讨论如何创建自己的新Node JS模块。

Node JS Platform has provided a technique to export the following things so that other modules can reuse them without redefining.

Node JS平台提供了一种导出以下内容的技术,以便其他模块可以重新使用它们而无需重新定义。

  • Variable变量
  • Function功能
  • Module模组

To export all these three, we have to use same technique. Node JS has provided an Object “exports” to do this.

要导出所有这三个,我们必须使用相同的技术。 Node JS提供了一个对象“导出”来执行此操作。

Syntax:

句法:

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.

现在,我们将通过一些示例逐一查看。 在这里,我们仅在理论上讨论它们。 在下一篇文章中,我们将采用一个实时场景并实现一个Node JS应用程序。

How to export a JavaScript Variable

如何导出JavaScript变量

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.

我们使用Node JS “导出”对象导出Node JS模块的变量,以便其他模块可以重用它。 实时地,我们不只是导出变量。 但是,仅出于练习目的,并且也为了使初学者更清楚,我们正在单独讨论此问题。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: pi.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:pi.js文件,其中包含以下内容。

var PI = 3.1416
exports.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.

在这里,我们将PI变量导出为ex​​ports.PI,名称为PI。 这意味着其他Node JS项目仅使用PI名称就可以非常轻松地使用此变量。

How to export a JavaScript Function:

如何导出JavaScript函数:

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.

我们甚至使用Node JS相同的“导出”对象来导出Node JS模块JavaScript函数,以便其他模块可以重复使用它。 我们可以实时执行此操作,但不建议始终使用。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:arthmetic.js文件,其内容如下。

function add(a,b){
return a + b;
}function sub(a,b){
return a - b;
}function mul(a,b){
return a * b;
}function div(a,b){
return a / b;
}exports.add = add
exports.sub = sub
exports.mul = mul
exports.div = div

Here we have exported all 4 JavaScript functions separately. That’s means other Node JS project can reuse them directly.

在这里,我们分别导出了所有4个JavaScript函数。 这意味着其他Node JS项目可以直接重用它们。

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.

这是一个简单的算术应用程序。 但是,如果我们使用一些实时Node JS应用程序,则可以观察到很多JavaScript文件,并且每个文件可能包含很多功能。 在这种情况下,分别导出每个功能都是非常繁琐的过程。

To resolve this issue, we need to use Exporting a Node JS module technique as described below.

要解决此问题,我们需要使用如下所述的导出Node JS模块技术。

How to export a Node JS Module:

如何导出Node JS模块:

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.

我们甚至使用Node JS相同的“导出”对象来导出Node JS模块,以便其他模块可以重用它。 建议您实时进行。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:arthmetic.js文件,其内容如下。

exports.arthmetic = {var PI = 3.1416;
function add(a,b){
return a + b;
}function sub(a,b){
return a - b;
}function mul(a,b){
return a * b;
}function div(a,b){
return a / b;
}
}

Here we have exported all 4 JavaScript functions and PI variable with just one exports statement. That’s means other Node JS project can reuse all functions and PI very easily.

在这里,我们仅用一个export语句导出了所有4个JavaScript函数和PI变量。 这意味着其他Node JS项目可以非常轻松地重用所有功能和PI。

导入Node JS模块 (Import a Node JS Module)

If we observe a Node JS Application or Module, it may dependent on other existing Node JS modules or our own Modules.

如果我们观察到Node JS应用程序或模块,则它可能依赖于其他现有的Node JS模块或我们自己的模块。

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:

如何导入Node JS模块:

We use same technique to import our modules or existing Node JS Modules.

我们使用相同的技术来导入我们的模块或现有的Node JS模块。

Node JS Platform has provided a function call “require()” to import one module into another.

Node JS平台提供了一个函数调用“ require()”,以将一个模块导入另一个模块。

Syntax:

句法:

Here module-name is our required Node JS module name.

这里module-name是我们必需的Node JS模块名称。

some-name is reference name to that module.

some-name是该模块的引用名称。

This require() call import the specified module and cached into the application so that we don’t need to import it again and again.

此require()调用导入指定的模块并缓存到应用程序中,因此我们不需要一次又一次地导入它。

Example:

例:

  • To import our own Node JS module

    var arthmetic = require("arthmetic");

    导入我们自己的Node JS模块

  • To import existing Node JS Module

    Import Node JS “express” module;

    var arthmetic = require("express");

    Import Node JS “mongoose” module;

    导入现有的Node JS模块

    导入Node JS“ express”模块;

    var arthmetic = require("express");

    导入Node JS“猫鼬”模块;

This require() call is similar to “import” statement in Java. We use import statement to import a package, class, interface etc into another class or interface.

此require()调用类似于Java中的“ import”语句。 我们使用import语句将包,类,接口等导入另一个类或接口。

Now we got some knowledge about how to export and import a Node JS module. We will use this knowledge to create our own Node JS Modules in next post.

现在我们了解了有关如何导出和导入Node JS模块的知识。 在下一篇文章中,我们将使用这些知识来创建自己的Node JS模块。

翻译自: https://www.journaldev.com/7599/nodejs-export-and-import-modules

nodejs导出导入

nodejs导出导入_NodeJS导出和导入模块相关推荐

  1. ES6的导入和导出模块

    ES6的导入和导出模块 1.导出模块 1.1普通的export导出方式 //先定义后导出 var name = 'zjl' var age = 18 function test1(){...} fun ...

  2. 使用node-xlsx组件实现excel导入和导出功能

    导入 1.npm install node-xlsx   模块 2.obj得到的就是excel的json数据,想怎么用就怎么用 let xlsx = require('node-xlsx');let ...

  3. 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理...

    在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能给系统用户更好的操作体验,也提高了用户录入数据的效率.我在较早时期的EasyUI的Web框架上, ...

  4. DLL 的导入与导出

    动态链接库(DLL)是从C语言函数库和Pascal库单元的概念发展而来的.所有的C语言标准库函数都存放在某一函数库中.在链接应用程序的过程中,链接器从库文件中拷贝程序调用的函数代码,并把这些函数代码添 ...

  5. (转)基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理...

    http://www.cnblogs.com/wuhuacong/p/4777720.html 在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能 ...

  6. python读取大文件csv_对python中大文件的导入与导出方法详解

    1.csv文件的导入和导出 通过一个矩阵导出为csv文件,将csv文件导入为矩阵 将csv文件导入到一个矩阵中 import numpy my_matrix = numpy.loadtxt(open( ...

  7. h5页面如何预览excel文件_如何使用JavaScript实现前端导入和导出excel文件?(H5编辑器实战复盘)...

    前言 最近笔者终于把H5-Dooring的后台管理系统初步搭建完成, 有了初步的数据采集和数据分析能力, 接下来我们就复盘一下其中涉及的几个知识点,并一一阐述其在Dooring H5可视化编辑器中的解 ...

  8. 如何使用JavaScript实现前端导入和导出excel文件(H5编辑器实战复盘)

    前言 最近笔者终于把H5-Dooring的后台管理系统初步搭建完成, 有了初步的数据采集和数据分析能力, 接下来我们就复盘一下其中涉及的几个知识点,并一一阐述其在Dooring H5可视化编辑器中的解 ...

  9. oracle表数据导出成unl文件,oracle的文本导入、导出技巧

    [IT168 服务器学院]在使用oracle时,总觉得oracle的导入,导出没有informix的load,unload好用,没办法,只能参照网上朋友的思路和informix的实现,写了几个脚本,希 ...

最新文章

  1. 如何做到让屏幕中的人不翼而飞?这个JavaScript项目告诉你该怎么做!
  2. tomcat7 https 拒绝连接_Ubuntu上运行Docker提示权限拒绝,如何处理?
  3. How to Analyze Java Thread Dumps--reference
  4. 【Android】Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法
  5. 运河杯交通违章 运行不起来
  6. linux redis客户端_你见过能把Redis的主从复制讲这么明白的吗?
  7. K3CLOUD数据权限授权
  8. 分段线性变换与直方图修正
  9. mysql 有外键 怎么插入数据_外键约束的表怎么插入数据
  10. 日期getUTCSeconds()方法以及JavaScript中的示例
  11. 嘉年华回顾丨杜小勇教授带你解密One Size Does not Fit All?
  12. SpringBoot四大核心之actuator——程序监控器
  13. Flutter 自定义 ImageButton
  14. altium designer PCB各层介绍+添加多层+设置正/负片+设置层的网络标号
  15. word公式编辑器出错及交叉引用问题
  16. 千锋培训python好吗?靠谱吗?
  17. WT2003H4-16S 语音芯片按键录音及播放应用解析
  18. VR视频为什么都是弯的?
  19. Python常见习题
  20. win10键锁定计算机,win10系统创建一键锁定计算机的快捷方式的操作方法

热门文章

  1. Ajax Post请求实例
  2. Atitit swt 4.3 4.4 4.5 新特性java attilax总结
  3. 20120321java
  4. 从华为“流程与IT管理部”看IT部门定位
  5. 转载:认识自我,把握机遇 —— 谢恩伟 (二)
  6. 送6个Gmail邀请!
  7. Sun Virtualbox说明文件的Bug
  8. C语言指针的高级操作
  9. 【opencv入门篇】 10个程序快速上手opencv【上】
  10. Linux CentOS 中安装 MySql