node.js笔记
The module system is what allows you to load functionality into your application and use it to do things like reading or writing from the file system, connect to a DB or start up a web server.
模块系统使您可以将功能加载到应用程序中,并使用它来执行诸如从文件系统读取或写入文件,连接到DB或启动Web服务器的操作。
In this blog, we will explain how to use it in three different ways and go step by step before inside all the knowledge required to build a note-taking app and then later we going to build one.
在此博客中,我们将说明如何以三种不同方式使用它,并逐步介绍构建笔记应用程序所需的全部知识,然后再构建一个。
- Load in core node modules, which are modules included with the node installation. 加载核心节点模块,这些模块是节点安装随附的模块。
- Load in third party modules written by other developers 加载其他开发人员编写的第三方模块
- Load in modules that we can create 加载我们可以创建的模块
fs.writeFileSync()
fs.writeFileSync()
This fs module of node accepts three-parameter,
该节点的fs模块接受三参数,
fs.writeFileSync( file, data, options )
Let’s try to use it to create a file. Open your code environment and create a folder, notes-app and inside create a file named app.js.
让我们尝试使用它来创建文件。 打开您的代码环境,并创建一个文件夹notes-app,并在其中创建一个名为app.js的文件。
First, we need to load in the module, and this is achieved by using require() function.
首先,我们需要加载模块,这是通过使用require()函数实现的。
Now we assigned the module to a variable that by convention we call fs. This module is build-in in node, so is going to work on all the node scripts.
现在,我们将模块分配给一个变量,按照惯例,我们将其称为fs。 该模块是内置在节点中的,因此将在所有节点脚本上工作。
Now we can call out function fs.writeFileSync(), giving the name of the file to create and the value we add to that file.
现在我们可以调用函数fs.writeFileSync(),给出要创建的文件的名称以及我们添加到该文件的值。

Now if we call this module in the terminal, we will see a file created with the text we write as the second argument in our function.
现在,如果在终端中调用此模块,我们将看到一个文件,该文件使用我们编写的文本作为函数中的第二个参数创建。
node app.js

Now let’s try to modify the file and write something else and see what’s happening.
现在,让我们尝试修改文件并编写其他内容,看看发生了什么。
fs.writeFileSync('note.txt', "My name is Mustafa")
Now we will see that our file override the previous value with the new one and that our functions work fine. If the file doesn’t exist, it will create one and add the message, and if the file exists, it will override the value with the new one.
现在,我们将看到我们的文件用新值覆盖了先前的值,并且我们的函数运行正常。 如果该文件不存在,它将创建一个并添加消息;如果该文件存在,它将用新的覆盖该值。
What about if we want to add a text next to the one we have loaded already? There is another module system build-in node to be able to do that.
如果要在已加载的文本旁边添加文本怎么办? 还有另一个模块系统内置节点可以做到这一点。
fs.appendFileSync()
Let’s try to happen a new text using this new function now.
现在,让我们尝试使用此新功能生成一个新文本。
fs.appendFileSync('note.txt', " and I love coding")
We need to leave some space at the begging of the text; otherwise, it will be directly attached to the previous one. Now run the file in the command line again, and you should see the following results:
我们需要在案文的开头留一些空间; 否则,它将直接附加到上一个。 现在,再次在命令行中运行该文件,您应该看到以下结果:

Good!! We have been able to use the “fs” module successfully to create a file with value and append another value to the file.
好!! 我们已经能够成功使用“ fs”模块创建具有值的文件,并将另一个值附加到文件中。
加载和导出模块(文件) (Load and export a module (file))
When working on a project, we won’t write all the code in the same file, and this would make it difficult to debug the project affecting the readability and the efficiency of our code.
在处理项目时,我们不会将所有代码都写在同一文件中,这将使调试项目变得困难,从而影响代码的可读性和效率。
Let’s see how we can write code in a file and use it in a different file.
让我们看看如何在文件中编写代码并在另一个文件中使用它。
File app1.js has a variable name, and need a function to print on the console the name variable.
文件app1.js具有变量名,并且需要一个函数在控制台上打印名称变量。

Now in a different file, that we call app2, we need to create a function that takes in a variable and print the value into the console, and we need to be able to export and import that function from app2 to app1.
现在在另一个名为app2的文件中,我们需要创建一个接受变量的函数并将其打印到控制台中,并且我们需要能够将该函数从app2导出和导入到app1。
To do this, we need to use another aspect of the module system.
为此,我们需要使用模块系统的另一个方面。
module.export
This is where we can define all the code that this file can share with other files.
在这里,我们可以定义该文件可以与其他文件共享的所有代码。

In app2 we created a function “printNameInTheConsole” that takes an argument and display the value in the console, simple.
在app2中,我们创建了一个函数“ printNameInTheConsole”,该函数接受一个参数并在控制台中简单显示该值。
Below we called “module.exports” and we assigned the value of the function. In app1 we need to require this module and assign the value to a variable that can be equal or different from the original.
下面我们称为“ module.exports”,并分配了函数的值。 在app1中,我们需要使用此模块并将值分配给一个可以与原始变量相等或不同的变量。

We used again the require function here but on a file, we created this time, and then we assigned the value to the variable, that we named differently from the original function just to show that works in any case and that the name can be different. However, is always better to keep the original name of the function. Now “printName” is our function from app2.js file ad we can simply call it and use it our file.
我们在这里再次使用了require函数,但是这次是在文件上创建的,然后我们将值赋给了变量,我们将其命名为与原始函数不同的名称,只是为了表明它在任何情况下都可以工作,并且名称可以不同。 但是,始终最好保留函数的原始名称。 现在“ printName”是app2.js文件广告中的函数,我们可以简单地调用它并使用它作为我们的文件。
翻译自: https://medium.com/swlh/node-js-module-system-part-1-note-taking-app-78b642242370
node.js笔记