This article will give you an in-depth understanding of the modularization, file system and environment variables in Node. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
1. Node.js modularization
1.0. Variable scope
(1). Variables defined using var or without keywords on the browser side belong to the global scope, that is, they can be accessed using the window object. [Related tutorial recommendations: nodejs video tutorial, Programming teaching]
<script>
var a = 100;
(function () {
b = 200;
})();
console.log(window.a, a);
console.log(window.b, b);
</script>
Copy after login
Result:
(2). There is no window object in Node.js
(3) In the interactive environment of Node.js, the defined variables belong to global. Global is similar to the window object on the browser side
## (4) There is a global object in the module (in the file), and the members defined using the keywords var, let, and const are not It belongs to the global object and is only valid in the current module. Objects defined without keywords belong to the global object.
var a=100;
b=200;
let c=300;
const d=400;
console.log(global);
console.log(global.a);
console.log(global.b);
console.log(global.c);
console.log(global.d);
Asynchronous rename(). The callback function has no parameters, but may throw an exception.
fs.ftruncate(fd, len, callback)
Asynchronous ftruncate(). The callback function has no parameters, but may throw an exception.
fs.ftruncateSync(fd, len)
Synchronized ftruncate()
fs.truncate(path, len, callback)
Asynchronous truncate(). The callback function has no parameters, but may throw an exception.
fs.truncateSync(path, len)
Synchronous truncate()
fs.chown(path, uid, gid, callback)
Asynchronous chown(). The callback function has no parameters, but may throw an exception.
fs.chownSync(path, uid, gid)
Sync chown()
fs.fchown(fd, uid, gid, callback)
Asynchronous fchown(). The callback function has no parameters, but may throw an exception.
fs.fchownSync(fd, uid, gid)
Sync fchown()
fs.lchown(path, uid, gid, callback)
Asynchronous lchown(). The callback function has no parameters, but may throw an exception.
fs.lchownSync(path, uid, gid)
Sync lchown()
fs.chmod(path, mode, callback)
Asynchronous chmod(). The callback function has no parameters, but may throw an exception.
fs.chmodSync(path, mode)
Sync chmod().
##fs.fchmod(fd, mode , callback)
Asynchronous fchmod(). The callback function has no parameters, but may throw an exception.
fs.fchmodSync(fd, mode)
Sync fchmod().
fs.lchmod(path, mode , callback)
Asynchronous lchmod(). The callback function has no parameters, but may throw an exception. Only available on Mac OS X.
fs.lchmodSync(path, mode)
Sync lchmod().
fs.stat(path, callback)
Asynchronous stat(). The callback function has two parameters err, stats, and stats is the fs.Stats object.
fs.lstat(path, callback)
Asynchronous lstat(). The callback function has two parameters err, stats, and stats is the fs.Stats object.
fs.fstat(fd, callback)
Asynchronous fstat(). The callback function has two parameters err, stats, and stats is the fs.Stats object.
fs.statSync(path)
Synchronize stat(). Returns an instance of fs.Stats.
fs.lstatSync(path)
Synchronize lstat(). Returns an instance of fs.Stats.
fs.fstatSync(fd)
Synchronize fstat(). Returns an instance of fs.Stats.
fs.link(srcpath, dstpath, callback)
Asynchronous link(). The callback function has no parameters, but may throw an exception.
fs.linkSync(srcpath, dstpath)
Sync link().
fs.symlink(srcpath, dstpath [, type], callback)
Asynchronous symlink(). The callback function has no parameters, but may throw an exception. The type parameter can be set to 'dir', 'file', or 'junction' (default is 'file').
fs.symlinkSync(srcpath, dstpath[, type])
Synchronize symlink().
fs.readlink (path, callback)
Asynchronous readlink(). The callback function has two parameters err, linkString.
fs.realpath(path[, cache], callback)
Asynchronous realpath(). The callback function has two parameters: err, resolvedPath.
fs.realpathSync(path[, cache])
Synchronize realpath(). Returns the absolute path.
fs.unlink(path, callback)
Asynchronous unlink(). The callback function has no parameters, but may throw an exception.
fs.unlinkSync(path)
Sync unlink().
fs.rmdir(path, callback)
Asynchronous rmdir(). The callback function has no parameters, but may throw an exception.
fs.rmdirSync(path)
Sync rmdir().
##fs.mkdir(path[, mode] , callback)
S asynchronous mkdir(2). The callback function has no parameters, but may throw an exception. mode defaults to 0777.
fs.mkdirSync(path[, mode])
Sync mkdir().
fs.readdir(path, callback)
Asynchronous readdir(3). Read the contents of the directory.
fs.readdirSync(path)
Synchronous readdir(). Returns a file array list.
fs.close(fd, callback)
Asynchronous close(). The callback function has no parameters, but may throw an exception.
fs.closeSync(fd)
Sync close().
fs.open(path, flags[, mode], callback)
Open the file asynchronously.
fs.openSync(path, flags[, mode])
Sync version of fs.open().
fs.utimes(path, atime, mtime, callback)
?
##fs.utimesSync(path, atime, mtime)
Modify file The timestamp of the file passed the specified file path.
fs.futimes(fd, atime, mtime, callback)
?
fs.futimesSync(fd, atime , mtime)
Modify the file timestamp, specified by the file descriptor.
fs.fsync(fd, callback)
Asynchronous fsync. The callback function has no parameters, but may throw an exception.
3.3. Obtain the environment variables in the system
Operation environment variables under the command line
3.3.1. View the current All available environment variables
Enter set to view.
3.3.2. Check an environment variable
Enter "set variable name". For example, if you want to check the value of the path variable, enter set path
3.3.3. Modify the environment variable
Note: all Modifications to environment variables under the cmd command line are only effective for the current window and are not permanent modifications. That is to say, when this cmd command line window is closed, it will no longer work.
There are two ways to permanently modify environment variables: one is to directly modify the registry, and the other is to set the system's environment variables through My Computer->Properties->Advanced (see details) .
1. Modify the environment variable
Enter "set variable name = variable content". For example, to set path to "d:\nmake.exe", just enter set path="d:\nmake.exe".
Note that modifying the environment variable means overwriting the previous content with the current content, not appending. For example, after I set the path above, if I re-enter set path="c", when I check the path again, the value is "c:", not "d:\nmake.exe";" c".
2. Set to empty:
If you want to set a certain variable to empty, enter "set variable name=".
For example, "set path=", then the path will be empty when you check it. Note that, as mentioned above, it only works in the current command line window. Therefore, when viewing the path, do not right-click "My Computer" - "Properties"...
3. Append content to the variable
Enter "set variable name=% variable name%; variable content". (Unlike 3, which is overlay). For example, to add a new path to path, enter "set path=%path%;d:\nmake.exe" to add d:\nmake.exe to path, and execute "set path=%path%; again" c:", then, when using the set path statement to view, there will be: d:\nmake.exe;c:, instead of only c: like in step 3.
3.3.4、一些常用的环境变量
%AllUsersProfile%: 局部 返回所有“用户配置文件”的位置。 {所有用户文件目录 – C:\Documents and Settings\All Users}
%AppData%: 局部 返回默认情况下应用程序存储数据的位置。 {当前用户数据文件夹 – C:\Documents and Settings\wy\Application Data}
%UserProfile%: 局部 返回当前用户的配置文件的位置。 {当前用户目录 – C:\Documents and Settings\wy}
%WinDir%: 系统 返回操作系统目录的位置。 {系统目录 – C:\WINDOWS}
假定当前的系统环境变量定义如下,注意JAVA_HOME:
m3.js
console.log(process.env.JAVA_HOME);
Copy after login
Output:
a,b have been defined in the system
Note that the current terminal is cmd, not powershell
The reason why a outputs 123 here is that the computer was not restarted after changing to 888.
4. Homework
4.1. Complete each class example based on the video.
4.2. Define a module circle.js. Define two methods in the middle module, one for calculating the circumference of a circle and one for calculating the area of a circle. Then define a module main.js that depends on the module circle. js, and call two methods in the module for calculation.
4.3. Define the port and host address host in the configuration file package.json, create a web server, reference the configuration information, and implement the switching function between the port and the host address.
4.4. Use config to complete 4.3
4.5. Use .env and dotenv to complete 4.3
4.6. Use system environment variables to complete 4.3
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of An article explaining modularity, file system and environment variables in Node in detail. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
If you find event ID 55, 50, 140 or 98 in the Event Viewer of Windows 11/10, or encounter an error that the disk file system structure is damaged and cannot be used, please follow the guide below to resolve the issue. What does Event 55, File system structure on disk corrupted and unusable mean? At session 55, the file system structure on the Ntfs disk is corrupted and unusable. Please run the chkMSK utility on the volume. When NTFS is unable to write data to the transaction log, an error with event ID 55 is triggered, which will cause NTFS to fail to complete the operation unable to write the transaction data. This error usually occurs when the file system is corrupted, possibly due to the presence of bad sectors on the disk or the file system's inadequacy of the disk subsystem.
How to deal with file system crash problems in Linux systems Introduction: With the continuous development of computer technology, the stability and reliability of the operating system are becoming more and more important. However, although Linux systems are widely regarded as a stable and reliable operating system, there is still the possibility of file system corruption. A file system crash may lead to serious consequences such as data loss and system abnormalities. Therefore, this article will introduce how to deal with file system crash problems in Linux systems to help users better protect their data and systems.
How to Optimize the Maintainability of Java Code: Experience and Advice In the software development process, writing code with good maintainability is crucial. Maintainability means that code can be easily understood, modified, and extended without causing unexpected problems or additional effort. For Java developers, how to optimize the maintainability of code is an important issue. This article will share some experiences and suggestions to help Java developers improve the maintainability of their code. Following standardized naming rules can make the code more readable.
1. Press win+r to enter the run window, enter [services.msc] and press Enter. 2. In the service window, find [windows license manager service] and double-click to open it. 3. In the interface, change the startup type to [Automatic], and then click [Apply → OK]. 4. Complete the above settings and restart the computer.
Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate
Python, as a high-level programming language, is widely used in software development. Although Python has many advantages, a problem that many Python programmers often face is that the maintainability of the code is poor. The maintainability of Python code includes the legibility, scalability, and reusability of the code. In this article, we will focus on how to solve the problem of poor maintainability of Python code. 1. Code readability Code readability refers to the readability of the code, which is the core of code maintainability.
fstab (FileSystemTable) is a configuration file in the Linux system, used to define the rules for mounting file systems when the system starts. The fstab file is located in the /etc directory and can be created manually or modified by an editor. Each line specifies a file system to be mounted. Each line has six fields, and their meanings are as follows: The file system device file or UUID can be used to specify the device of the file system to be mounted. The UUID is a unique identifier. The UUID of the device can be obtained through the blkid command. 2. Mount point: Specify the directory to which the file system is to be mounted, which can be an absolute path (such as /mnt/data) or a relative path (such as ../data). 3. File system class
Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to