This article describes the usage of the os module in detail explained by the API. Share it for your reference, as follows:
os is a basic module of Node, providing some operating system-related attribute interfaces.
passconst os = require('os');
Refer to the Os module in a way, let's take a look at what functions are available in Os
illustrate:
A string constant that defines the end-of-line flags related to the operating system: The value on POSIX is \n, and the value on Windows is \r\n
demo:
('abc'+ +'123'); //abc //123
()
illustrate:
This method returns a string to illustrate the CPU architecture of the current running environment.
Possible values are: 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64', and 'x86'
demo:
(()); //x64
illustrate:
This constant is an object that contains system constants such as error codes and processing signals. For details, please see: OS constants
demo:
(); // { UV_UDP_REUSEADDR: 4, //libuv constant// errno: // { E2BIG: 7, // EACCES: 13, // EADDRINUSE: 48, // EADDRNOTAVAIL: 49, // EAFNOSUPPORT: 47, // EAGAIN: 35, // EALREADY: 37, // EBADF: 9, // EBADMSG: 94, // EBUSY: 16, // ECANCELED: 89, // ECHILD: 10, // ECONNABORTED: 53, // ECONNREFUSED: 61, // ECONNRESET: 54, // EDEADLK: 11, // EDESTADDRREQ: 39, // EDOM: 33, // EDQUOT: 69, // EEXIST: 17, // EFAULT: 14, // EFBIG: 27, // EHOSTUNREACH: 65, // EIDRM: 90, // EILSEQ: 92, // EINPROGRESS: 36, // EINTR: 4, // EINVAL: 22, // EIO: 5, // EISCONN: 56, // EISDIR: 21, // ELOOP: 62, // EMFILE: 24, // EMLINK: 31, // EMSGSIZE: 40, // EMULTIHOP: 95, // ENAMETOOLONG: 63, // ENETDOWN: 50, // ENETRESET: 52, // ENETUNREACH: 51, // ENFILE: 23, // ENOBUFS: 55, // ENODATA: 96, // ENODEV: 19, // ENOENT: 2, // ENOEXEC: 8, // ENOLCK: 77, // ENOLINK: 97, // ENOMEM: 12, // ENOMSG: 91, // ENOPROTOOPT: 42, // ENOSPC: 28, // ENOSR: 98, // ENOSTR: 99, // ENOSYS: 78, // ENOTCONN: 57, // ENOTDIR: 20, // ENOTEMPTY: 66, // ENOTSOCK: 38, // ENOTSUP: 45, // ENOTTY: 25, // ENXIO: 6, // EOPNOTSUPP: 102, // EOVERFLOW: 84, // EPERM: 1, // EPIPE: 32, // EPROTO: 100, // EPROTONOSUPPORT: 43, // EPROTOTYPE: 41, // ERANGE: 34, // EROFS: 30, // ESPIPE: 29, // ESRCH: 3, // ESTALE: 70, // ETIME: 101, // ETIMEDOUT: 60, // ETXTBSY: 26, // EWOULDBLOCK: 35, // EXDEV: 18 }, // signals: // { SIGHUP: 1, // SIGINT: 2, // SIGQUIT: 3, // SIGILL: 4, // SIGTRAP: 5, // SIGABRT: 6, // SIGIOT: 6, // SIGBUS: 10, // SIGFPE: 8, // SIGKILL: 9, // SIGUSR1: 30, // SIGSEGV: 11, // SIGUSR2: 31, // SIGPIPE: 13, // SIGALRM: 14, // SIGTERM: 15, // SIGCHLD: 20, // SIGCONT: 19, // SIGSTOP: 17, // SIGTSTP: 18, // SIGTTIN: 21, // SIGTTOU: 22, // SIGURG: 16, // SIGXCPU: 24, // SIGXFSZ: 25, // SIGVTALRM: 26, // SIGPROF: 27, // SIGWINCH: 28, // SIGIO: 23, // SIGINFO: 29, // SIGSYS: 12 // } // }
()
illustrate:
This method returns an array of objects containing information for each logical CPU core.
demo:
(()); // [ { model: 'Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz', // speed: 2900, // times: { // user: 96756760, // Number of milliseconds spent by CPU in user mode// nice: 0, // milliseconds spent by the CPU in good mode// sys: 80507720, // Number of milliseconds spent by the CPU in system mode// idle: 606147830, // Number of milliseconds spent by the CPU in idle mode// irq: 0 // Number of milliseconds spent by the CPU in interrupt request mode// } // }, // { model: 'Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz', // speed: 2900, // times: { user: 43796970, nice: 0, sys: 37796280, idle: 701811920, irq: 0 } // }, // { model: 'Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz', // speed: 2900, // times: { user: 94060830, nice: 0, sys: 68641950, idle: 620702410, irq: 0 } // }, // { model: 'Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz', // speed: 2900, // times: { user: 45641410, nice: 0, sys: 39136540, idle: 698627210, irq: 0 } // } ]
()
illustrate:
This method returns a string indicating the byte order of the binary compilation environment. The possible values are: 'BE'(Big-Endian) Big-endian mode, 'LE'(Little-Endian) Little-endian mode
Big-endian mode: refers to the high bytes of data being stored in the low address of memory, and the low bytes of data being stored in the high address of memory.
Little-endian mode: refers to the high bytes of data being stored in the high address of memory, and the low bytes of data being stored in the low address of memory.
Take 0x12345678 as an example:
demo:
(()); //LE
()
illustrate:
This method returns the number of bytes in the free system memory as an integer
demo:
(()); //7155720192
()
illustrate:
This method returns the current user's home directory as a string
demo:
(()); // /Users/xiaoqiang
()
illustrate:
This method returns the hostname of the operating system as a string
demo:
(()); //
()
illustrate:
This method returns an array containing 1, 5, 15 minutes average load.
demo:
(()); //[ 2.57080078125, 2.38037109375, 2.30908203125 ] Return [0, 0, 0] under windows
()
illustrate:
This method returns an object containing the network interface assigned to the network address.
demo:
(()); // { // lo: [ // { // address: '127.0.0.1', //The IPv4 or IPv6 address assigned// netmask: '255.0.0.0', //IPv4 or IPv6 subnet mask// family: 'IPv4', //IPv4 or IPv6// mac: '00:00:00:00:00:00', //The MAC address of the network interface// internal: true, // If the network interface is a loopback or a similar remote unusable interface, the value is true, otherwise it is false// cidr: '127.0.0.1/8' // IPv4 or IPv6 address with routing prefix assigned in CIDR notation. If the netmask parameter is not available, the property is null// }, // { // address: '::1', // netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', // family: 'IPv6', // mac: '00:00:00:00:00:00', // internal: true, // cidr: '::1/128' // } // ], // eth0: [ // { // address: '192.168.1.108', // netmask: '255.255.255.0', // family: 'IPv4', // mac: '01:02:03:0a:0b:0c', // internal: false, // cidr: '192.168.1.108/24' // }, // { // address: 'fe80::a00:27ff:fe4e:66a1', // netmask: 'ffff:ffff:ffff:ffff::', // family: 'IPv6', // mac: '01:02:03:0a:0b:0c', // internal: false, // cidr: 'fe80::a00:27ff:fe4e:66a1/64' // } // ] // }
()
illustrate:
This method returns the compiled operating system platform, the value may be: 'aix','darwin','freebsd','linux','openbsd','sunos','win32'
demo:
(()); // darwin
()
illustrate:
This method returns a string that specifies the distribution of the operating system.
demo:
(()); // 17.2.0
()
illustrate:
This method returns a string that specifies the distribution of the operating system.
demo:
(()); // /var/folders/xv/4dbb00000gn/T
()
illustrate:
This method returns the number of bytes in all system memory as an integer.
demo:
(()); //17179869184
()
illustrate:
This method returns a string indicating the name of the operating system
demo:
(()); // Darwin
()
illustrate:
This method returns the operating system's online time within a few seconds
demo:
(()); // 1293306
([options])
illustrate:
This method is used to obtain information of the current valid user
This method passes an encoding parameter to specify the encoding method of the return value. The default utf8 can also be set to: buffer.
demo:
(()); // { // uid: 501, // gid: 20, // username: 'xiaoqiang', // homedir: '/Users/xiaoqiang', // shell: '/bin/bash' // } (({encoding: 'buffer'})); // { // uid: 501, // gid: 20, // username: , // homedir: , // shell: // }
I hope this article will be helpful to everyone's programming.