node.js 模块
In my previous post, we have discussed about “How to export and import a Node JS Module” with syntax and some examples. If you haven’t gone through my previous posts, please go through them first and come back to this point because now onwards we are going to use that knowledge to develop all our examples.
在我以前的文章中,我们讨论了“ 如何导出和导入Node JS模块 ”以及语法和一些示例。 如果您还没有阅读我以前的文章,请先阅读它们,然后回到现在为止,因为从现在开始,我们将利用这些知识来开发所有示例。
We have already discussed that Node JS is Modular Platform. Node JS Platform contains many modules or packages developed by Node JS or Third-Party clients. I guess you have some curiosity to know about the implementation of those existing Node JS Modules.
我们已经讨论过Node JS是模块化平台。 Node JS平台包含许多由Node JS或第三方客户端开发的模块或软件包。 我想您会对这些现有Node JS模块的实现有所了解。
Node JS Platform has provided an approach to create our own modules (if required) to match our project requirements. It is very easy and simple to create our own Node JS Modules.
Node JS平台提供了一种方法来创建我们自己的模块(如果需要)以符合我们的项目要求。 创建我们自己的Node JS模块非常简单容易。
We are going to discuss the approach to create our own Node JS Modules and also how to reuse that module as a Node JS Library in other module in this post.
在本文中,我们将讨论创建自己的Node JS模块的方法,以及如何将该模块用作Node JS库中的其他模块。
Real-time Scenario:
As Developer or even in academic, I guess we all have developed a simple program to print “Fibonacci sequence”.
实时场景 :
作为开发人员,甚至是学术人员,我想我们都已经开发出了一个简单的程序来打印“斐波那契数列”。
Fibonacci sequence is a sequence of numbers in which first two numbers are 1, 1 and each subsequent number is the sum of the previous two numbers. For example 1,1,2,3,5,8,13 and so on.
斐波那契数列是一个数字序列,其中前两个数字为1、1,每个后继数字是前两个数字的总和。 例如1,1,2,3,5,8,13等等。
To develop custom or our own Node JS Modules or Packages, we need to do the following two steps:
要开发自定义或我们自己的Node JS模块或包,我们需要执行以下两个步骤:
- Create new Node JS Module 创建新的Node JS模块
- Publish our own Node JS Module 发布我们自己的Node JS模块
We will discuss these two steps in detail by using the scenario explained above.
我们将使用上述场景详细讨论这两个步骤。
如何创建新的Node JS模块 (How to create new Node JS Module)
We are going to develop a new Node JS Module with “fabonacci” name. We are going to use Enide Studio IDE 2014 to develop this example.
我们将开发一个名为“ fabonacci”的新Node JS模块。 我们将使用Enide Studio IDE 2014来开发此示例。
Please use the following steps to create new Node JS Module:
请使用以下步骤创建新的Node JS模块:
- Create a Node JS Project (Please refer “Node JS Basic Examples With Node REPL (CLI) and Node IDE” post for complete steps)
Enter Project name: fabonacci
Select Template to use: none/empty
带有Node REPL(CLI)和Node IDE的Node JS基本示例 ”) 输入项目名称:fabonacci
选择要使用的模板:无/空
- It creates a new Node JS Project with one README.md file as shown below;
- Create a Java Script file named “fabonacci.js”
创建一个名为“ fabonacci.js”的Java脚本文件
- Provide implementation to generate numbers in Fibonacci Sequence as below.
fabonacci.js
/*** JournalDEV * Fabonacci Sequence : 1,1,2,3,5,8,13,...*/
function fabonacci(num1,num2){return num1+num2;
}
提供实现在斐波那契数列中生成数字的实现,如下所示。 fabonacci.js
- Export this module functionality to use in other Node JS Modules. Node JS Platform has provided “exports” object to do this as shown below;
exports.fabonacci = fabonacci;
Please refer my previous post “Node JS: Export and Import Modules” for more details on exports object.
请参阅我以前的文章“ Node JS:导出和导入模块 ”以获取有关导出对象的更多详细信息。
- Create a package.json file at root directory as shown below. If you have already gone through my previous posts to create “simple” application, simply copy package.json from that project and modify accordingly. Otherwise, please go through the following two steps.
Step 1: “New” >> “Other” to open “New Wizard” window
Step 2: “JSON” >> “JSON File”
Click on “Next” button and provide File name as package.json
Click on Finish button. It creates empty JSON file. Copy below JSON content to package.json file and save it.
package.json
{"name": "fabonacci","version": "1.0.0","description": "fabonacci sequence","main": "fabonacci","author": "JournalDEV","engines":{"node":"*"}
}
If you don’t understand about this package.json file, please go through my previous post “Importance of package.json in Node JS Applications”.
步骤1:“新建” >>“其他”打开“新建向导”窗口
步骤2:“ JSON” >>“ JSON文件”
单击“下一步”按钮,并提供文件名作为package.json
单击完成按钮。 它会创建一个空的JSON文件。 将下面的JSON内容复制到package.json文件并保存。
package.json
如果您不了解这个package.json文件,请阅读我以前的文章“ package.json在Node JS Applications中的重要性 ”。
- Now our Node JS project looks like below;
如何使用我们的Node JS模块 (How to use our Node JS Module)
First we will create a new Node JS Project like “fabonacci-client” then we will use “fabonacci” module to test it.
首先,我们将创建一个新的Node JS项目,例如“ fabonacci-client”,然后将使用“ fabonacci”模块进行测试。
- Create new Node JS Project like “fabonacci-client”.
This step is known as publishing our own newly created Node JS Module into other Node JS Module.
此步骤称为将我们自己新创建的Node JS模块发布到其他Node JS模块中。
- Now “fabonacci-client” includes “fabonacci” module as shown below.
现在,“ fabonacci-client”包括“ fabonacci”模块,如下所示。
- Create a Javascript file to test “fabonacci” module functions.
fabonacci-client.js
/*** JournalDEV * Fabonacci Sequence : 1,1,2,3,5,8,13,...*/
/*** New node file*/
var fab = require("fabonacci");
var num = fab.fabonacci(1,1);
console.log(num);
创建一个Javascript文件以测试“ fabonacci”模块功能。 fabonacci-client.js
- Finally project looks like below image;
- Run fabonacci-client.js in Enide Studio IDE 2014 as “Node Application”.
Change fabonacci-client.js as shown below and run it again to see next number in the Fibonacci Sequence.
如下所示更改fabonacci-client.js并再次运行以查看Fibonacci序列中的下一个数字。
var num = fab.fabonacci(1,2);
As a Java Developer, I guess you have already done this kind of activities in your projects. When we have a common or reusable component in our project requirements, then we will develop this component as a separate project, package this component into a JAR and place this JAR file in other Project module’s CLASSPATH, so that those dependent Project modules will reuse that component.
作为Java开发人员,我想您已经在项目中完成了此类活动。 当我们在项目需求中有一个通用或可重用的组件时,我们将把该组件开发为一个单独的项目,将该组件打包到一个JAR中,然后将此JAR文件放置在其他Project模块的CLASSPATH中,以便那些依赖的Project模块可以重用该组件。零件。
We did similar kind of approach in this post too. We have created a new Node JS Module – fabonacci, packaged that module, add this module into our new Node JS Project and reuse those functions defined in fabonacci in our new Node JS Project.
我们在这篇文章中也做了类似的方法。 我们创建了一个新的Node JS模块– fabonacci,将该模块打包,将该模块添加到新的Node JS Project中,并在新的Node JS Project中重用fabonacci中定义的那些功能。
翻译自: https://www.journaldev.com/7608/how-to-create-node-js-reusable-modules
node.js 模块