Specific use of node.js global variables

Specific use of node.js global variables

Global Object

All modules can be called

  1. global: represents the global environment of Node, similar to the window object in the browser.
  2. process: points to Node's built-in process module, allowing developers to interact with the current process.
  3. For example, if you directly enter node in DOS or terminal window, you will enter the NODE command line mode (REPL environment). To exit, enter process.exit();
  4. console: points to the built-in console module of Node, which provides standard input and standard output functions in the command line environment.

Global Functions

Timer functions: There are 4 timer functions: setTimeout(), clearTimeout(), setInterval(), clearInterval().

require: used to load modules.

It was often seen in King Qi's house and heard several times in Cui Jiu's hall.

Global variables

  • _filename: points to the name of the script currently running.
  • _dirname: points to the directory where the currently running script is located.

Quasi-global variables

The local variables inside the module point to different objects depending on the module, but they are applicable to all modules and can be regarded as pseudo-global variables, mainly module, module.exports, exports, etc.

The module variable refers to the current module. The module.exports variable represents the interface exported by the current module. When other files load the module, they actually read the module.exports variable.

  • module.id The module identifier, usually the module's file name.
  • module.filename The filename of the module.
  • module.loaded returns a boolean value indicating whether the module has finished loading.
  • module.parent returns the module that uses this module.
  • module.children returns an array of other modules that this module uses.

Global sample code

insert image description here

// Include the full path of the file name console.log(__filename);
// The path to the file (excluding the file name)
console.log(__dirname);

// Timing function, usage is similar to the timing function in the browser var timer = setTimeout(function(){
    console.log(123);
},1000);

setTimeout(function(){
    clearTimeout(timer);
},2000);

// There is no window object in Node.js, but there is a similar object global, which can be omitted when accessing global members
global.console.log(123456);

// argv is an array. By default, the first two items are: the path of the Node.js environment; the full path of the currently executed js file // Starting from the third parameter, it represents the command line parameters console.log(process.argv);
// Print the current system architecture (64-bit or 32-bit)
console.log(process.arch);

This is the end of this article about the specific use of node.js global variables. For more relevant node.js global variables content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Usage of Node.js http module
  • Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency
  • Understanding what Node.js is is so easy
  • AsyncHooks asynchronous life cycle in Node8
  • Nodejs error handling process record
  • The whole process of node.js using express to automatically build the project
  • How to use shell scripts in node
  • The core process of nodejs processing tcp connection
  • Detailed explanation of Nodejs array queue and forEach application
  • Comparing Node.js and Deno

<<:  How to quickly delete all tables in MySQL without deleting the database

>>:  Detailed explanation of using Docker to build a development environment for Laravel and Vue projects

Recommend

Tomcat parses XML and creates objects through reflection

The following example code introduces the princip...

Vue implements verification whether the username is available

This article example shares the specific code of ...

How to build Nginx image server with Docker

Preface In general development, images are upload...

Introduction to the process of building your own FTP and SFTP servers

FTP and SFTP are widely used as file transfer pro...

SQL-based query statements

Table of contents 1. Basic SELECT statement 1. Qu...

Introduction to the usage of props in Vue

Preface: In Vue, props can be used to connect ori...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

Detailed installation process and basic usage of MySQL under Windows

Table of contents 1. Download MySQL 2. Install My...

How to display percentage and the first few percent in MySQL

Table of contents Require Implementation Code dat...

Summary of the use of element's form elements

There are many form elements. Here is a brief sum...

Common interview questions and answers for web designer positions

1. What are the templates for ASP.NET Web applicat...

Examples of correct judgment methods for data types in JS

Table of contents Preface Can typeof correctly de...

js to realize the production method of carousel

This article shares the specific code for js to r...

Several ways to improve the readability of web pages

1. Use contrasting colours. The contrast here ref...