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”

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

    创建新的Node JS项目,例如“ fabonacci-client”。

  • Copy package.json file from “fabonacci” to “fabonacci-client” and modify accordingly.

    package.json

    {"name": "fabonacci-client","version": "1.0.0","description": "fabonacci sequence client","main": "fabonacci-test","author": "JournalDEV","engines":{"node":"*"}
    }

    将package.json文件从“ fabonacci”复制到“ fabonacci-client”并进行相应的修改。

    package.json

  • Open command prompt and goto “fabonacci-client” folder and run command as shown in the image.

    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 模块

node.js 模块_如何创建Node JS可重用模块相关推荐

  1. node环境变量_实际使用Node环境变量的方法如下

    node环境变量 Environment variables are a fundamental part of Node development, but for some reason I nev ...

  2. 下载perl的mysql模块_安装用于操作MySQL的Perl模块

    在我使用Webmin(version 1.480)管理FreeBSD主机上的MySQL数据库服务器时出现: "警告:您的系统未安装Perl 模块 DBI 和 DBD::mysql,Webmi ...

  3. python中常用模块_工作中用过的Python常用模块:(基于3.x)

    内置模块: sys 用于提供对解释器相关的访问及维护.例如:sys.argv --传参 sys.platform --返回系统平台名称 sys.version --查看python版本 os 用于提供 ...

  4. modules node 太大了_如何将Node.js Docker镜像大小减小10倍

    对应用程序进行Docker化非常简单,有效,但是优化Docker Image的大小是棘手的部分. Docker易于使用,但是一旦应用程序开始扩展,镜像大小就会呈指数增长.通常,大多数情况下,应用程序的 ...

  5. three.js 交互_如何使用Three.js创建交互式3D角色

    three.js 交互 View demo 查看演示 Download Source 下载源 Ever had a personal website dedicated to your work an ...

  6. d3.js图表_如何使用D3.js建立历史价格图表

    d3.js图表 逐步可视化财务数据集的方法 (A step by step approach towards visualizing financial datasets) It is a chall ...

  7. brain.js 时间序列_免费的Brain JS课程学习JavaScript中的神经网络

    brain.js 时间序列 The last few years, machine learning has gone from a promising technology to something ...

  8. uibot在子程序执行js失败_使用BotUI搭建js机器人

    前言 最近在家闲的我,偶然机会看到一个博客个人页面的机器人有点意思.当时我就兴奋极了,向那位博主讨教一番,才知道这是BotUI BotUI 是一个有趣的聊天 JS 框架.自动回复文字.图片.视频的JS ...

  9. python爬虫遇到js加密_当爬虫遇到js加密

    目标网站:http://tool.liumingye.cn/music/?type=qq&name=%E4%BD%A0%E5%A5%BD(测试) 在谷歌浏览器输入该网址之后,打开F12开发者工 ...

最新文章

  1. Oracle中,使 CREATE TABLE AS SELECT 支持ORDER BY
  2. 编程语言圣经(卷一)
  3. C++输入、输出优化模板整理
  4. 数据结构学习笔记(六)链表算法题
  5. 10. 二进制中1的个数(C++版本)
  6. Java 中判断char 是否为空格 和空
  7. CSS控制显示超出部分,用省略号显示
  8. ESP32-SPI接口bl0942驱动
  9. Podfile 文件模板
  10. 如何破解HIT的孤岛效应,一起关于患者主索引的案例分享
  11. 平方和误差函数--代价函数(机器学习)
  12. 多路耦合器(有源分离器)在无线通讯中的应用
  13. CSDN拒绝好友的私信内容信息太弓虽了
  14. Shell - cp
  15. 范蠡传(司马迁-史记)
  16. 07_Flask闪现 get_flashed_messages()
  17. 视觉组考核——装甲板识别
  18. opencv-python 实现角点检测和棋盘角点检测
  19. UcosII移植、调度、功能、运行流程解析
  20. C语言自学完备手册(07)——printf()输出格式详解

热门文章

  1. [转载] 【python】内置函数 slice()
  2. [转载] plt.hist()和numpy.histogram()的学习
  3. MATLAB函数 zp2tf详解
  4. phpstrom 安装
  5. Dapper.NET—轻量ORM
  6. [带权并查集] Jzoj P1503 体育场
  7. 使用C语言和Java分别实现冒泡排序和选择排序
  8. Betsy Ross Problem
  9. 浅谈C#深拷贝,浅拷贝
  10. 数据结构笔记(三十三)--二叉排序树的插入与生成