node.js 模块_如何创建Node.js模块

本教程介绍了如何在Node.js中创建模块,模块是一组可供外部应用使用的JavaScript函数和对象。你将学习如何创建一个包含颜色建议的模块,通过数组存储颜色并提供随机选择颜色的函数。此外,你还会了解如何使用REPL测试模块,以及如何通过npm管理本地模块,包括链接本地模块以实现实时更新。
摘要由CSDN通过智能技术生成

node.js 模块

The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。

介绍 (Introduction)

In Node.js, a module is a collection of JavaScript functions and objects that can be used by external applications. Describing a piece of code as a module refers less to what the code is and more to what it does—any Node.js file or collection of files can be considered a module if its functions and data are made usable to external programs.

在Node.js中, 模块是可由外部应用程序使用JavaScript函数和对象的集合。 将一段代码描述为模块意味着更少的是什么代码,而更多的是它的功能-如果任何Node.js文件或文件集合可以使其功能和数据可用于外部程序,则可以将其视为模块。

Because modules provide units of functionality that can be reused in many larger programs, they enable you to create loosely coupled applications that scale with complexity, and open the door for you to share your code with other developers. Being able to write modules that export useful functions and data will allow you to contribute to the wider Node.js community—in fact, all packages that you use on npm were bundled and shared as modules. This makes creating modules an essential skill for a Node.js developer.

因为模块提供了可以在许多大型程序中重用的功能单元,所以它们使您能够创建松散耦合的应用程序,这些应用程序可以随着复杂性而扩展,并为您与其他开发人员共享代码打开了大门。 能够编写导出有用功能和数据的模块将使您能够为更广泛的Node.js社区做出贡献-实际上,您在npm上使用的所有软件包都捆绑并共享为模块。 这使得创建模块成为Node.js开发人员的一项基本技能。

In this tutorial, you will create a Node.js module that suggests what color web developers should use in their designs. You will develop the module by storing the colors as an array, and providing a function to retrieve one randomly. Afterwards, you will run through various ways of importing a module into a Node.js application.

在本教程中,您将创建一个Node.js模块,该模块建议Web开发人员在设计中应使用什么颜色。 您将通过将颜色存储为数组并提供随机检索颜色的功能来开发模块。 之后,您将通过各种方式将模块导入Node.js应用程序。

先决条件 (Prerequisites)

第1步-创建模块 (Step 1 — Creating a Module)

This step will guide you through creating your first Node.js module. Your module will contain a collection of colors in an array and provide a function to get one at random. You will use the Node.js built-in exports property to make the function and array available to external programs.

此步骤将指导您创建第一个Node.js模块。 您的模块将在数组中包含一组颜色,并提供一种随机获取颜色的功能。 您将使用Node.js内置的exports属性使函数和数组可用于外部程序。

First, you’ll begin by deciding what data about colors you will store in your module. Every color will be an object that contains a name property that humans can easily identify, and a code property that is a string containing an HTML color code. HTML color codes are six-digit hexadecimal numbers that allow you to change the color of elements on a web page. You can learn more about HTML color codes by reading this HTML Color Codes and Names article.

首先,首先要确定要存储在模块中的有关颜色的数据。 每种颜色都是一个对象,其中包含人类可以轻松识别的name属性,以及一个code属性,该属性是包含HTML颜色代码的字符串。 HTML颜色代码是六位十六进制数字,使您可以更改网页上元素的颜色。 您可以阅读此HTML颜色代码和名称文章,以了解有关HTML颜色代码的更多信息。

You will then decide what colors you want to support in your module. Your module will contain an array called allColors that will contain six colors. Your module will also include a function called getRandomColor() that will randomly select a color from your array and return it.

然后,您将决定要在模块中支持的颜色。 您的模块将包含一个名为allColors的数组,该数组将包含六种颜色。 您的模块还将包含一个名为getRandomColor()的函数,该函数将从数组中随机选择一种颜色并将其返回。

In your terminal, make a new folder called colors and move into it:

在您的终端中,新建一个名为colors文件夹并移至其中:

  • mkdir colors

    mkdir颜色
  • cd colors

    cd颜色

Initialize npm so other programs can import this module later in the tutorial:

初始化npm,以便其他程序可以在本教程的后面部分中导入此模块:

  • npm init -y

    npm初始化-y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值