In the recent articles we studied node.js web module and then express framework. We also studied how express module is helpful in implementing the RESTful API. In this article we are going to briefly review Node.JS OS module. This module contains functions that are helpful in obtaining operating system related information. For instance, you can get OS name, OS architecture, the total amount of OS memory, the amount of free memory etc. Following are some of the most commonly used node.js OS module functions.
We have defined the usage of the most commonly used methods of OS module. Let’s see their working in action with the help of an example. Take a look at the following piece of code.
var os = require("os"); console.log('OS Type : ' + os.type()); console.log('OS platform : ' + os.platform()); console.log('OS Archi : ' + os.arch()); console.log('OS Uptime : ' + os.uptime() ); console.log('OS Release : ' + os.uptime() ); console.log('OS free memory : ' + os.freemem() + " bytes."); console.log('OS Total memory : ' + os.totalmem() + " bytes.");
The output of the above code depends upon the operating system on which the code is running. However, on my system the first function returns Windows_NT, the platform is Win32 and the architecture is X64. This means that I have a 64 bit windows operating system running on my machine.
OS module is extremely helpful in those cases where you program same application for multiple operating systems. You can place checks which implicate that if the underneath operating system is windows, do this else do that. In the next article, we will study path module in detail.