热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

node.js模块_如何创建NodeJS可重用模块

node.js模块Inmypreviouspost,wehavediscussedabout“HowtoexportandimportaNodeJSModule”withsynta

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模块或包,我们需要执行以下两个步骤:

  1. Create new Node JS Module

    创建新的Node JS模块
  2. 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”
    New-Node.js-File

    创建一个名为“ 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”.
    NPM-install-command

    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.
    NPM-module-eclipse-project

    现在,“ 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 模块



推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
author-avatar
君琪2010_207
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有