Chapter III Nodejs Basic Knowledge (Part I)

Source: Internet
Author: User

This chapter focuses on some Nodejs basics:

1. What is the console in Nodejs, how to output the standard output stream to the console and the standard error output stream;

2. What are the global scopes in Nodejs, and what global functions and global variables are provided beforehand in the Nodejs;

3. What is the event handling mechanism in Nodejs and the event loop, how to specify the event trigger and the callback function to execute when the event is canceled in Nodejs;

4. How to debug the application using the debugger in Nodejs;



3.1 Nodejs in the console


In Nodejs, use the console object to represent the console. In Nodejs, the output of the standard output stream and the standard error output stream can be made to the console through various methods of the console object.


3.1.1 Console.log method

Console.log ("This is a Test String")

the line code will output a line to the console, "This is a test string." String.

You can save the line code in a script file, and then run the script file in a command-line window using the command shown below, such as

Node Apps.js

1 for redirect standard output stream



Starting with the second argument, output all string Console.log ("%s", "Hoge", "foo") sequentially; Output Console.log ("%d", "Hoge", {foo: "foo"}) after converting the object to a string after Hoge foo//; The output is Hoge ({foo: "foo"})//Converts the numeric value to a string output, starting with the second parameter, outputting all numeric Console.log ("%d", "Hoge") sequentially;  Output nan//output percent console.log ("%", "Hoge"); Output% Hoge

You can use various operators in the Console.log method to output the results, or you can use the ToString method to convert the value of a variable to a string and then output it.

Console.log ("the");  Using arithmetic operators, the output is 4console.log (2/0);   Output Infinityvar A=1;var b=2;console.log (a=b); <span style= "color: #FF0000;" The > Assignment operator outputs the 2,A variable value to 2</span>//and converts both the numeric variable A and the value variable B to a string, using the + operator to link two string values after the output Console.log (a.tostring () + B.tostring ()); var a=1;var b= "2"; Console.log (A+B); Converts a value to a string and links to other strings, with the output 12;var a = 1;var B = 2;console.log (a==b); Using the comparison operator, output falseconsole.log (a==1&b==2); Using logical operators, output true In addition, in Nodejs, the Console.info method replaces the Console.log method, both of which are identical to the way they are used <span style= "FONT-SIZE:18PX;" ><strong>3.1.2 Console.error Method </strong> This method is used to output a standard error output stream, output a line of error messages to the console </span><pre name= " Code "class=" JavaScript ">//console.err" ("This is an error string."); <span style= "FONT-SIZE:18PX;" > Meaning and. Log use is the same, can also be saved as a JS script file, and then run in a DOS window, here is not too much demo </span>


Open Error.log File

module.js:338 throw err; ^error:cannot Find module ' D:\nodetest\script.js ' at Function.module._resolvefilename (module.js:336:15) at Functio N.module._load (module.js:278:25) at Function.Module.runMain (Module.js:501:10) at startup (Node.js:129:16) at No De.js:814:3
Other than that As with the Console.log method, you can specify the format of the output string in the Console.error method by using parameters that are the same as the parameters used in the Console.log method, you can use various operators to calculate the results, or you can use the ToString () Method converts the value of a variable to a string and then outputs it. Similarly, Console.warn can replace the Console.error method

3.1.3 Console.dir Method

var user=new Object (); User.name= "Cuiwjava"; User.getname=function () {return this.name;}; User.setname=function () {this.name=name;}; Console.dir (user);//save in a script file, then node xxx.js  




Well, such a file will be generated!!

3.1.4 Console.time and Console.timeend method

In Nodejs, when you need to count a piece of code execution time, you can use the Console.time method with the Console.timeend method, where the former is used to identify the start time, which marks the end time, and the number of milliseconds elapsed between them is output in the console:


Console.time (label); console.timeend (label);

Both methods use a parameter that can be any string, but the parameter strings used by the two methods must be the same for the correct time to be counted.

3.1.5 Console.trace method

The current stack information is output as a standard error message.

<span style= "FONT-SIZE:18PX;" >console.trace (label);</span>



3.1.6 Console.assert method

Evaluates the execution result of an expression and throws a message string exception if the execution result is false: Assertionerror;

3.2 Global scope and global functions in node. js

Variables defined in a module, functions or methods are only available in the module, but can be passed to the outside of the module through the use of the exports object

Global variables in 3.2.1nodejs

There is a global scope in Nodejs that allows you to define variables, functions, or classes that can be used without loading by any module. At the same time, some global methods and global classes are predefined.

In addition, in Nodejs, a global object is defined that represents the universal namespace in Nodejs, and any global variable, function, or object is a property value for that object.

Console.log (global);

This is all global variables, functions, or objects

Then use the global call to view Console.log (globals);



Chapter III Nodejs Basic Knowledge (Part 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.