node. JS Basic article

Source: Internet
Author: User

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

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.