node. js other Modules

Source: Internet
Author: User
Tags deprecated eol mail exchange

Ching Ming Holiday was very fast, 3 days said no no, the hot day to clean up the room today, looking very comfortable. Saturday understand the next process management processes module, because the process management knowledge is also more, today, some other modules to understand, process management this piece slowly learn.

One, OS module

The OS module is primarily for obtaining operating system information.

varOS = require (' OS ');//returns a string that points to the default temp directory of the operating system if you need to temporarily store the file and then delete it can be placed in thisConsole.log ("Tmpdir: \ T" +Os.tmpdir ());//depending on the architecture of the machine, for big-endian and small-end encodings, respectively, the Be or LeConsole.log ("endianness: \ T" +os.endianness ());//host name of the machineConsole.log ("hostname: \ t" +os.hostname ());//operating system type String typeConsole.log ("type: \t\t" +Os.type ());//platform name String typeConsole.log ("Platform: \ T" +os.platform ());//Architecture String TypeConsole.log ("Arch: \t\t" +Os.arch ());//operating System release versionConsole.log ("Release: \ T" +os.release ());//how long the operating system has been running and returns the timestamp in secondsConsole.log ("Uptime: \ T" +os.uptime ());//in Unix-based systems, returns a system payload that contains [1,5,15] minutes worth of arrayConsole.log ("Loadavg: \ T" +os.loadavg ());//system memory capacity byte unitsConsole.log ("Totalmem: \ T" +Os.totalmem ());//available system memory byte unitsConsole.log ("Freemem: \ T" +Os.freemem ());//EOL contains the appropriate end-of-line characters for the operating system, which makes one application cross-platform compatible when working with string dataConsole.log ("EOL: \ T" +OS. EOL);//returns an array of objects that describe the model (model), speed, and time, which contains the time that the CPU has spent on user, Nice, sys, DLE, IRQConsole.log ("CPUs: \t\t" +json.stringify (Os.cpus ()));//returns an array of objects that describe the address and family (address family) that are bound to each network interface in the operating systemConsole.log ("Networkinterfaces:" +json.stringify (Os.networkinterfaces ()));
Tmpdir:c:\users\admini~1\appdata\local\tempendianness:lehostname:lenovo-PCtype:Windows_NTplatform:win32arch:x64release:10.0.10240Uptime:857903.0353439loadavg:0,0,0Totalmem:4202979328Freemem:609837056Eol:cpus: [{"Model": "Intel (R) Core (TM) i5-4200m CPU @ 2.50GHz", "Speed": 2494, "times": {"user": 14228468, "nice": 0, "sys": 16551359, " Idle ": 266524781," IRQ ": 3004875}},{" model ":" Intel (R) Core (TM) i5-4200m CPU @ 2.50GHz "," Speed ": 2494," times ": {" User " : 13634187, "nice": 0, "sys": 17665812, "idle": 266004171, "IRQ": 5612906}},{"model": "Intel (R) Core (TM) i5-4200m CPU @ 2.50GHz "," Speed ": 2494," times ": {" user ": 14396953," nice ": 0," sys ": 17213171," idle ": 265694046," IRQ ": 2041984}},{" Model ":" Intel (R) Core (TM) i5-4200m CPU @ 2.50GHz "," Speed ": 2494," times ": {" user ": 15294390," nice ": 0," sys ": 19912703," Idle ": 262097078," IRQ ": 2563078}}]networkinterfaces: {"WLAN": [{"Address": "Fe80::ec29:33b8:7d3d:28e0", "netmask": "Ffff:ffff:ffff:ffff::", "Family": "IPv6", "Mac": "48:5A: b6:d5:44:8f "," ScopeID ": +," internal ":false},{"Address": "192.168.1.103", "netmask": "255.255.255.0", "Family": "IPv4", "Mac": "48:5a:b6:d5:44:8f", "internal":false}], "Loopback pseudo-interface 1": [{"Address": ":: 1", "netmask": "Ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "Family": "IPv6", "Mac": "00:00:00:00:00:00", "ScopeID": 0, "internal":true},{"Address": "127.0.0.1", "netmask": "255.0.0.0", "Family": "IPv4", "Mac": "00:00:00:00:00:00", "internal":true}], "Local connection *": [{"Address": "2001:0:5ef5:79fb:3079:3794:c272:4da6", "netmask": "Ffff:ffff:ffff:ffff::", "Family": " IPv6 "," Mac ":" 00:00:00:00:00:00 "," ScopeID ": 0," internal ":false},{"Address": "Fe80::3079:3794:c272:4da6", "netmask": "Ffff:ffff:ffff:ffff::", "Family": "IPv6", "Mac": " 00:00:00:00:00:00 "," ScopeID ": 9," internal ":false}]}process finished withExit Code 0

Second, util module

1. Formatting strings

//Util.format (format,[....)//Util.format (format,[....) Receives a formatted string as the first argument and returns a formatted string//placeholder Type:%s: Specified string%d: Specifies a numeric value (but an integer or floating-point number)%j: Specifies an object that can be converted to a string by JSON//%: If the% is left blank, it is not used as a placeholder//at the same time there are several points to note://1. Placeholders are longer than parameters, and extra placeholders such as%s are not replaced//2. Parameters are longer than placeholders, extra arguments are converted to strings, and then concatenated with a space separator//3. If the first parameter is not a format string, each argument is converted to a string, concatenated with a space delimiter, and returnedvarResult=util.format ('% s=%s ', ' abc '); Console.log (result); result=util.format ('%s=%s ', ' abc ')); Console.log (result); result=util.format ('%s ', ' abc ', ' Def ')); Console.log (result); result=util.format (' 1 ', ' abc ', ' Def ')); Console.log (result);
% s=abcabc=%SABC def1 ABC def4 Apr 19:40:47-1 with exit code 0

2. Check the object type

View Util module source code has IsArray (), Isboolean (), IsNull (), isnullorundefined (), Isnumber (), isstring (), Issymbol (), isundefined (), Isregexp (), IsObject (), IsDate (), IsError (), Isfunction (), isprimitive (), Isbuffer () functions.

var  Result=util.isarray ([+]); Console.log (result); result =Util.isnull (result); Console.log (result); Result=util.isdate (new  Date ()); Console.log (result);
true false true   with exit code 0

3. Synchronous write output stream

Util provides debug (), error (), puts (), print (), log (), and other methods to synchronize the data to stdout, stderr. This means that the process remains blocked until the data is written out. However, when looking at its source code, it has been suggested that these methods have been replaced by other methods.

' Util.exec is deprecated. Use child_process.exec instead. ' ' Util.print is deprecated. Use Console.log instead. ' ' Util.puts is deprecated. Use Console.log instead. ' ' Util.debug is deprecated. Use Console.error instead. ' ' Util.error is deprecated. Use Console.error instead. ' ' Util.pump is deprecated. Use Readablestream.pipe instead. '

4. Converting a JavaScript object to a string

The Util.inspect (Object,[options]) method examines the object and returns the string representation of the object. This method is also mentioned in the above blog.

Options can contain the following properties:

Showhidden:true, the object's non-enumerable property is also converted to a string, by default false

Depth: Restricts the depth of the check process traversal when formatting attributes are also objects. can prevent infinite loops and prevent complex objects from spending a lot of CPU cycles. The default is 2. If it is empty (null), it can be recursive indefinitely.

Colors:true, the output uses the ANSI color code style, which defaults to false.

Custominspect:false, any custom inspect () functions defined by the object being inspected are not called, by default true.

You can attach the custom inspect () function to an object to control the output.

var  Obj={first: ' Brad ', Last: ' Dayley '};obj.inspect=function(depth) {    return ' { Name: "' +this. first+" "+this. last+ '"} ';}; Console.log (Util.inspect (obj));
{name: "Brad Dayley" with exit code 0

5. Inheriting features from other objects

This feature has been known before. Http://www.cnblogs.com/5ishare/p/5299493.html

Iii. use of the DNS module

DNS modules can resolve DNS domain names, find domains, or do direction lookups. The following methods are common:

Lookup (domain,[family],callback): resolves the domain name. The family property can be 4, 6, or null. 4: Parse the first found a (IPV4) record, 6: Resolves the first found AAAA (IPV6) record, null: Both parse, default null. The callback function first receives an error, and the second receives an array of IP addresses. function (error,addresses)

Resolve (domain,[rrtype],callback): Resolves a domain name to an array of records specified by the Rrtype type. function (error,addresses)

A:ipv4 Address (default)

Aaaa:ipv6 address.

MX: Mail exchange record.

TXT: Text recording.

Srv:srv Records.

PTR: Reverse IP lookup.

NS: Name server record.

CNAME: Canonical name record.

Resolve4 (Domain,callback): With Dns.resolve (). Only a record is parsed.

Resolve6 (Domain,callback): With Dns.resolve (). Only AAAA records are parsed.

Resolvemx (domain,callback): ibid., only resolves MX records

Resolvetxt (Domain,callback): ibid. txt record only

Resolvesrv (domain,callback): ibid., only resolves SRV records

Resolvens (domain,callback): IBID., only NS records resolved

Resolvecname (Domain,callback): ibid., only the CNAME is parsed.

Reverse (Ip,callback): direction lookup of the IP address. If an error occurs, the callback function receives an error object and receives an array of domains if the lookup succeeds. function (Error,dimains).

var dns = require (' DNS '); Console.log ("Resolving www.baidu.com ...");d ns.resolve4 (  function  (err, addresses) {  Console.log (false, ') ");  Addresses.foreach (function  (addr) {    function  (err, domains) {      Console.log (' Reverse for ' + addr + ': ' + json.stringify (domains));    }   );});
"14.215.177.38""14.215.177.37" for 14.215.177.38 for 14.215.177.37   with exit code 0

node. js other Modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.