Nodejs Getting Started learning notes

Source: Internet
Author: User

Tag: Handler ons post color function parameter file contents log

I. Blocking and non-blocking

The blocking is performed sequentially. There is a sequence of priorities.

Input.txt file content is:     I was blocking
var=require("FS");  var= fs.  Readfilesync(' input.txt ');  Console.  Log(data.  ToString()); console.  Log("program execution ends!") ); 
The output is:
    I was blocking
End of program execution !

Rather than blocking is not required in order, so if you need to handle the parameters of the callback function, we need to write in the callback function.

Input.txt file content is:     I am non-blocking
var fs = require ("FS"); Fs.readfile (thefunction  (err, data)    {if  return  console.error (err);    Console.log (Data.tostring ());}); Console.log ("program execution ends!");

The output is:
   Program execution ends!
I'm a non-blocking

two. Events

//Introducing the Events modulevarEvents = require (' Events ');//Create a Eventemitter objectvarEventemitter =Newevents. Eventemitter ();
*********************************************//1.To create an event handlervarConnecthandler =functionconnected () {Console.log (' Connection succeeded. ‘); }//2.binding Connection Event handlersEventemitter.on (' Connection ', Connecthandler);//3.Triggering connection EventsEventemitter.emit (' Connection '); *********************************************
* Pay attention to the order of writing in the split line, otherwise it will fail!
of course: Create an event handler without putting it in 2
Console.log ("program execution is complete. ");


Nodejs Getting Started learning notes

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.