热门标签 | 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 模块



推荐阅读
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
  • 本文介绍了如何使用vue-awesome-swiper组件,包括在main.js中引入和使用swiper和swiperSlide组件,以及设置options和ref属性。同时还介绍了如何在模板中使用swiper和swiperSlide组件,并展示了如何通过循环渲染swipes数组中的数据,并使用picUrl属性显示图片。最后还介绍了如何添加分页器。 ... [详细]
  • 本文介绍了如何在Mac上使用Pillow库加载不同于默认字体和大小的字体,并提供了一个简单的示例代码。通过该示例,读者可以了解如何在Python中使用Pillow库来写入不同字体的文本。同时,本文也解决了在Mac上使用Pillow库加载字体时可能遇到的问题。读者可以根据本文提供的示例代码,轻松实现在Mac上使用Pillow库加载不同字体的功能。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
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社区 版权所有