Home  >  Article  >  Web Front-end  >  Summary of console usage in node.js_node.js

Summary of console usage in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:26:421618browse

Copy code The code is as follows:

//Create app.js page
// 1: Page code
console.log("log information");
//Execute this file (node ​​app.js) in the page and you will see the log information in the console: "log information"
//Execute in another way: node app.js 1>info.txt (1 represents redirecting the standard output stream);
//At this time, you will see an info.txt file in the same directory as app.js, with "log information" in it.
//Two: Output all strings in order
console.log("%s","first","second");
//Output result: first second
//3. Convert the object to an ordinary string and then execute
console.log("%s","guoyansi",{name:"Dr. Sisi"});
//Output result: guoyansi { name: 'Dr. Sisi' }
//four:
//Convert string as numeric value
console.log("%d","25.6");
//Output result: 25.6
console.log("%d","guoyansi");
//Output result:guoyansi
//5 Output%
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
//There will be an additional err.txt file in the same directory. There is also "guoyansi is error"
in the file. //7 Directly start a file javascript.js that does not exist on the command line, like this:
// node javascript.js 2>info.txt
//Output result: There will be an additional file info.txt in the directory where the command line is located;
//The contents of the info.txt file are as follows

/*
module.js:340
throw err;
^
Error: Cannot find module 'E:nodegysjavascript.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
*/
//Eight: The usage of console.warn is the same as the usage of console.error()
//Nine: console.time() and console.timeEnd() output the execution time of the intermediate code (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 location as standard error information.
* */
var obj={
Name:"guoyansi",
Age:23,
Eat:function(){}
}
console.trace(obj);
//Output result:

I don’t know if you can understand it, but I can’t understand it anyway.

1 //Ten: console.assert() evaluates the expression result. If the execution result of the expression is false, output a message string and throw an AssertionError exception

2 console.assert("g"==="s","g is not equal to s");

Isn’t it very simple? . Anyway, I feel like I can’t understand it, haha

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn