Summary of console usage in node. js and node. jsconsole usage
Copy codeThe Code is as follows:
// Create an app. js page
// 1: Page code
Console. log ("log information ");
// Run the node app. js file on the page. The log information is displayed on the console: "log information"
// Execute in another way: node app. js 1> info.txt (1 indicates redirecting the standard output stream );
// At this time, an info.txt file will be viewed in the app.js Directory, which contains "log information ".
// 2: Output all strings in order
Console. log ("% s", "first", "second ");
// Output result: first second
// 3. Convert the object to a normal string and then execute
Console. log ("% s", "guoyansi", {name: "Dr. Sisi "});
// Output result: guoyansi {name: 'Dr. ss '}
// 4:
// Convert a string as a numeric value
Console. log ("% d", "25.6 ");
// Output result: 25.6
Console. log ("% d", "guoyansi ");
// Output result: guoyansi
// Five outputs %
Console. log ("% ");
// Output result: %
Console. log ("%", "gys ");
// Output result: % gys
// 6. output the console. error information to the file.
// Page code:
Console. error ("guoyansi is error ");
// Use node app. js 2> err.txt to start this page
// One More err.txt file in the same directory will contain "guoyansi is error"
// Directly start a non-existing file javascript. js in the command line, as shown in the following code:
// Node javascript. js 2> info.txt
// Output result: An info.txt file is generated in the directory of the command line;
// The content in the info.txt file is as follows:
/*
Module. js: 340
Throw err;
^
Error: Cannot find module 'e: \ node \ gys \ javascript. js'
At Function. Module. _ resolveFilename (module. js: 338: 15)
At Function. Module. _ load (module. js: 280: 25)
At Function. Module. runMain (module. js: 497: 10)
At startup (node. js: 119: 16)
At node. js: 906: 3
*/
// 8: the usage of console. warn is the same as that of console. error ().
// 9: the execution time of the intermediate code output by console. time () and console. timeEnd () (Note: The parameters of time and timeEnd must be exactly the same)
Console. time ("for Loop time :")
Var a = 0;
For (var I = 0; I <10000000000000; I ++ ){
A ++;
}
Console. timeEnd ("for Loop Time :")
/*
* 10. The console. trace () method outputs the stack information at the current position as a standard error message.
**/
Var obj = {
Name: "guoyansi ",
Age: 23,
Eat: function (){}
}
Console. trace (obj );
// Output result:
I don't know if you can understand it.
1 // 10: console. assert () evaluates the expression result. If the execution result of this expression is false, a message string is output and an AssertionError exception is thrown.
2 console. assert ("g" = "s", "g is not equal to s ");
Isn't it easy, huh .. I cannot understand it, haha.