Global: Global object in node.js are available in all the modules and are scoped locally to their respective modules.
Some of the global objects are:
The above objects and require function can be accessed everywhere without importing.
Process: The process object is a global object that gives the information about and control over node.js process. As it is global, it can be used in the project without importing it from any module.
It is an instance of EventEmitter class and has many useful methods that help us in knowing more about the processes that happened and also about currently undergoing the process.
Important events of process:
beforeExit Event: This event is triggered before when Node.js event loop is getting hallow and has no additional work scheduled. In general cases, the Node.js process will exit when there is no process left, but a listener registered on the ‘beforeExit’ can make asynchronous calls.
This code is rendered first. Process beforeExit event with code. Process exit event with code.
Exit Event: The ‘exit’ event is emitted when the Node.js process is about to exit as a result of either:
- The process.exit() method being called explicitly.
- The Node.js event loop no longer has any additional work to perform.
There is no way to prevent the exiting of the event loop at this point, and once all ‘exit’ listeners have finished running the Node.js process will terminate.
code excution is goint to end
Apart from the above two main events, there are also many more events that come with process object.
Buffer: The Buffer class in Node.js is made to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren’t resizable and have a whole bunch of methods specifically for binary data.
Creating a buffer:
var buffer = Buffer.alloc(6);
This will print out 6 bytes of zero
var buffer = Buffer.from("Welcome to GeeksforGeeks!", "utf-8");
This will print out a chain of values in utf-8
Writing to a buffer: If it contains two arguments, the first argument is the data and the second argument is the type of encoding.
This will print out 13 as size of buffer
Reading a buffer: We can use toString() method to read a buffer.
- Nodejs | Automatic restart NodeJs server with nodemon
- Node.js Buffer.buffer Property
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- How Machine Learning and Artificial Intelligence Will Impact Global Industries in 2020?
- How to remove all Global Modules in Node.js ?
- Global keyword in Python
- What is the Python Global Interpreter Lock (GIL)
- How to declare a global variable in PHP?
- What are the AngularJs Global API?
- What is GAV (Global as View)?
- Node.js | Global Objects
- Signup Form Using Nodejs and MongoDB
- Difference between NodeJS and AngularJS
- Nodejs | authentication using Passportjs and passport-local-mongoose
- 7 Famous Apps Built on NodeJS: Reasons and Benefits
- Which One is Most Demanding Back-End Web Framework between Laravel , NodeJS and Django ?
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.