Node.js tools + path module module module -OS

 

Node.js os module provides basic operating system functions.

os.tmpdir ()
returns the operating system's default temporary folder.

os.endianness ()
returns the byte order of the CPU, it is possible "BE" or "LE".

os.hostname ()
Returns the host name of the operating system.

os.type ()
Returns the operating system name

os.platform ()
operating system name when compiling return

os.arch ()
Returns the operating system CPU architecture, possible values are "x64", "arm" and "ia32".

os.release ()
returns the release version of the operating system.

os.uptime ()
Returns the operating system running time, in seconds.

os.loadavg ()
returns an array of 5, 15-minute average load comprises.

os.totalmem ()
Returns the total amount of system memory, in bytes.

os.freemem ()
returns the amount of free memory operating system, in bytes.

os.cpus ()
returns an array of objects, each containing information about the installed CPU / core: model, speed (MHz), the time (containing a user, nice, sys, idle, and the irq CPU / core milliseconds using Object).

os.networkInterfaces ()
to obtain list of network interfaces.

os.EOL
defines constants for the operating system of the end of lines.

var os = require("os");

// the CPU endianness 
the console.log (os.endianness ());
 // operating system name of 
the console.log (os.type ());
 // operating system name 
console.log (os.platform ());
 // system memory amount 
the console.log (os.totalmem () + "bytes" );
 // operating system, the amount of free memory 
console.log (os.freemem () + "bytes ");

 

 

Node.js path module provides a number of small tools for working with file paths

path.normalize (p)
normalized path, attention '..' and '.'.

path.join ([path1] [, path2
] [, ...]) to the connection path. The main purpose of this method is that the current system correctly path delimiter, Unix systems is "/", Windows systems "\."

path.resolve ([from ...], to
) the  to  parameter parsing an absolute path, the path to a given sequence is being processed from right to left, after each path are sequentially parsed until an absolute path has been constructed . For example, given the sequence of path segments: / foo, / bar, baz , is called path.resolve ( '/ foo', ' / bar', 'baz') returns / bar / baz.

path.resolve ( '/ foo / bar', './baz' );
 // Returns: '/ foo / bar / baz' 

path.resolve ( '/ foo / bar', '/ tmp / File /' );
 // return: '/ tmp / File' 

path.resolve ( 'wwwroot', 'static_files / PNG /', '../gif/image.gif' );
 // if the current working directory is / home / myself / node , 
// returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

path.isAbsolute (path)
is determined parameter  path  whether or not the absolute path.

path.relative(from, to)

Will be converted to an absolute path to a relative path, relative path is returned from from to TO (based on the current working directory).

On Linux:

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// 返回: '../../impl/bbb'

On Windows:

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// 返回: '..\\..\\impl\\bbb'

path.dirname (p)
return portion representing the folder path, similar to the Unix dirname same command.

path.basename (p [, ext])
Returns the last part of the path. With the Unix command bashname similar.

path.extname (p)
partial returns path to the file name extension, i.e., the last path '' after. If the path does not include a '.' Or the path contains only one '' and the '.' Character is the first path, this command returns an empty string.

path.parse (pathString)
Returns a string object path.

path.format (pathObject)
Returns a string from the object path, and path.parse contrary.

path.sep
file path separator internet, '\\' or '/'.

path.delimiter
platform separator,; or ':'.

path.posix
provide the above path method, but always posix-compatible way interaction.

path.win32
provide the above path method, but always win32 compatible way interaction.

var path = require("path");

// Format path 
the console.log (path.normalize ( '/ Test / test1 // 2slashes / 1slash / Tab / ..')); // '/ Test / test1 // 2slashes / 1slash / Tab / ..'

// connection path 
the console.log (path.join ( '/ Test', 'test1', '2slashes / 1slash', 'Tab', '..')); // \ Test \ test1 \ 2slashes \ 1slash

// converted to absolute path 
console.log (path.resolve ( "main.js")); // C: \ the Users \ YG1ST \ mynode \ main.js

// extension path file 
console.log (path.extname ( "main.js")); // .js

 

Guess you like

Origin www.cnblogs.com/chenyingying0/p/12467189.html