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

How to quickly copy large files under Linux

Copy data When copying data remotely, we usually ...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

How to Rename Multiple Files at Once in Linux

Preface In our daily work, we often need to renam...

Detailed steps to install MySQL 5.7 via YUM on CentOS7

1. Go to the location where you want to store the...

Detailed tutorial on setting password for MySQL free installation version

Method 1: Use the SET PASSWORD command MySQL -u r...

MySQL time type selection

Table of contents DATETIME TIMESTAMP How to choos...

Some tips on website design

In fact, we have been hearing a lot about web des...

HTML basics summary recommendation (paragraph)

HTML Paragraph Paragraphs are defined by the <...

vue-router history mode server-side configuration process record

history route History mode refers to the mode of ...

Some tips on using the HTML title attribute correctly

If you want to hide content from users of phones, ...

Detailed explanation of Tomcat's commonly used filters

Table of contents 1. Cross-domain filter CorsFilt...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...