Log output mode
Node Test.js 2>error.log 1>info.log
If you need a log file append node Test.js 2>>error.log 1>>info.log
If you are using Sublimetext-nodejs, you need to modify the following nodes in Nodejs.sublime-build (depending on your operating system)
"cmd": ["taskkill/f/im Node.exe & node $file 2>>error.log 1>>info.log", ""]
If not set, the default output to the system console
Log syntax
Console.log (' Server running at http://127.0.0.1:8888/'); Console.info (' Text:%s! ', message); Console.error (' This is a Error '); Console.warn (' This is a warn ');
Warn or error cannot be distinguished in the log in node. js, uniformly saved in the Exception Log
Output a piece of code execution time
Console.time ("Hi"); Console.log ("It works!"); Console.timeend ("Hi");
http
A simple HTTP Service
var http = require (' http '); Http.createserver (function (request, response) { Response.writehead (200, {') Content-type ': ' text/html '}); Response.End (' Hello world\n ');}). Listen (8888); Console.log (' Server running at http://127.0.0.1:8888/');
A simple HTTP client
Http.get ({ hostname: ' localhost ', port:8888, path: '/', agent:false //Create a new agent just for This one request}, function (res) { var data = '; Res.on (' Data ', function (chunk) { data + = Chunk.tostring (); }); Res.on (' End ', function () { Console.log ("Data is:" +data);} ); Http.get (' http://localhost:8888 ', function (res) { var data = '; Res.on (' Data ', function (chunk) { data + = Chunk.tostring (); }); Res.on (' End ', function () { Console.log ("Data is:" +data);} );
Node. JS Foundation article