node. JS API Learning Note (i)

Source: Internet
Author: User

This article has been published in my blog.

Terminal (terminal)

Speaking of the use of the Linux system is generally known to the elder brother, I understand: Similar to putty these SSH tools through the software to implement remote control host, for our users, it will display information and can receive information and processing, this is the terminal. As for what pseudo-terminal should be software implementation so that a set of input can support multiple soft terminals.

There are 3 terminal modules in node. js, respectively: STDIO, TTY, REPL.

First, STDIO

If I don't understand the mistake. The console should be an input output of the NODDJS standard, typically outputting information to stdout or stderr.

Second, TTY

This TTY seems to be the same as the pseudo-terminal, but it is not clear, but not very main listen to say is ready to give up.

Third, REPL

This is an interactive interpreter, as for what interactivity does not explain. This repl provides a way to interact with the state and information that node is running. For example, we enter the command line under Windows:

Node
>consolg (' Hello world! ');

This will print out the Hello World character.

Iv. use of the console

The above uses Console.log (), the statement prints the information, then we can use this to view a lot of information, such as the value of variables, stack information and assertions.

Console.log ([data],[...]): The first parameter is a string, you can take the same as the C language placeholder, followed by optional, I see some information on the web, placeholder support%s (string),%d (number),%j (JSON). Examples are as follows:

    Without placeholders and parameters, output: Hello World    console.log (' Hello World ');    With placeholders, but without parameters. Output: Hello     console.log (' Hello%s ');    Take the placeholder along with the parameters. Output: Hello World    console.log (' Hello%s ', ' World ');    Output: Age    was Console.log (' Age is%d ', +);    Output: JSON data:{' age ': $    console.log (' json data:%j ', {age:30});    Output: {age:30}    console.log ({age:30});    Time mark, logo lable start    console.time (' lable ');    The time stamp, which identifies the lable end. Output lable:100ms (can calculate intermediate processing time)    console.timeend (' lable ');    Prints information about the current stack lable. Output trace:lable----------------------    console.trace (' lable ');    Assertion syntax: Console.assert (Expression,[message])    //output assertionerror: Abnormal    Console.assert (false, ' abnormal slightly ');

V. Buffer

The appearance of this buffer is basically to solve the situation that JS itself is unfriendly to the binary system. We all know that the existence of Nodejs depends on the network, requests for different resources and even files are used in binary, using INODEJS specifically to provide this mechanism to handle the byte stream.

Currently I have collected buffer supported encoding: ' ASCII ': this speed is fast; ' UTF8 ': supports Unicode characters; ' Base64 ': Base64 string encoding; ' Utf16le ', ' ucs2 ', ' hex '.

Create a buffer instance, now know that there are 4 kinds;

The first specifies the size, which is fixed and does not change    var buf = new Buffer (8) because of write writes;    Buf.write (' Luoliang ');    Console.log (BUF);    The second type: array initialization. Output: <buffer fe ff 00>. It can be seen that the remainder of the 256 is obtained, and that it is not rounded up entirely by the rounding    var buf1 = new Buffer ([1.2,3.7,254,255,256]);    Console.log (BUF1);    The third type: string initialization and coding, default UTF8    var buf2 = new Buffer (' Chinese ');    Output: Chinese    console.log (buf2.tostring ());    The following will output garbled    buf2 = new Buffer (' Chinese ', ' ASCII ');    Console.log (Buf2.tostring ());    The following will output: 9 E4b8ade59bbde4baba    var buf3 = new Buffer (' Chinese ');    Console.log (buf3.length);    Console.log (buf3.tostring (' hex '));    The length of the different encoded objects above is different, this need to pay special attention to the following can be done in their own experiment        //There is a slice to create a buffer object, but this I basically useless, indicating that the intercept string created buffer    var buf4 = New Buffer (' Chinese ');    var buf5 = Buf4.slice ();    Output: Chinese    console.log (buf5.tostring ());    Output: Guo    buf5 = Buf4.slice (3,6);

This note is here first, then continue!

node. JS API Learning Note (i)

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.