node. JS Basics

Source: Internet
Author: User

One, the console in node. JS

There are many ways to control the console in node. js. For example:

The Console.log method is used to output the standard output stream, which displays a line of strings in the console. You can also specify the format of the output string.

The console.error is used to output the standard error output stream.

Console.dir is used to view the contents of an object and output the contents of the image to the console.

Console.time marks the start time.

Console.timeend Mark End time. Together, they can count the execution time of a piece of code. The unit is in milliseconds.

The console.trace is used to output the stack information at the current location as a standard error message.

Console.assert is used to evaluate the results of an expression.

Second, global scope and global functions in node. JS

in the In node. js , variables, functions, and methods defined in a module are available only in that module, but can be passed to the outside of the module through the use of the export object.

in the A global object is defined in node. js, which represents a globally named space in node. js, and any global variable, function, object is a property value for that object.

SetTimeout function and cleartimeout function

the setTimeout defined in node. JS is similar to setTimeout in the client js script , and executes the callback function after a number of milliseconds in the current moment.

SetInterval function and clearinterval function

The setinterval defined in node. JS is similar to setinterval in a client-side JS script , after which the callback function is executed after each number of milliseconds after the current time has elapsed.

Both SetTimeout and setinterval return a Timer object with the unref and ref methods.

var timer = setinterval (function () {console.log (' ZFC ')},3000);

Timer.unref ();// Cancel the call of the callback function, the console has no output.

Timer.ref ()/// You can also restore the call to the callback function, which is the console prints the ZFC every milliseconds .

Iii. global functions and objects related to modules

use the Require function to load the module.

use the require.resolve function to query the full module name.

Require.cache This object represents a cache of all the modules that have been loaded.

Four, __filename variables and __dirname variables

__filename: Within any module, you can use the __filename variable to get the file name of the current module file with the full absolute path.

__dirname: Inside any module, you can use the __dirname variable to get the complete absolute path to the directory where the current module file resides.

V. event handling mechanism and event ring mechanism

in the A Eventemitter class is defined in node. JS's Event module for implementing various event handling . All objects that are likely to trigger an event are instance objects that inherit the subclass of the Eventemitter class.

Eventemitter defines a number of methods:

AddListener (event, listener): binds the event handler function to the specified event.

On (event, listener): ibid.,AddListener alias.

Once (event, listener): Specifies an event handler function that executes only once for the specified event.

RemoveListener (event, listener): The event handler function is dismissed for the specified event.

Removealllistener ([Event]): all event handler functions are dismissed for the specified event.

Setmaxlisteners (n): Specifies the number of mouths of the event handler function.

Listeners (event): gets all event handlers for the specified event.

Emit (event,[arg1],[arg2],[...]): manually trigger the specified event.

Vi. node. js Debugger

in the Node . JS provides a debugger that you can use in the command-line interface to perform some debugging, such as displaying code, variables, return values for functions, and so on.


This article is from "Time" blog, please make sure to keep this source http://timeblog.blog.51cto.com/8650972/1588382

node. JS Basics

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.