Execute code console.log (' Hello World ');
2, callback function because node is an asynchronous event-driven platform, we often need to use callback functions in our code. The following is a classic example of a callback function app:
SetTimeout (function () {
Console.log (' callback is called ');},2000);
We passed an anonymous function to the settimeout function and a call time of 2000 (milliseconds), after running the program for 2 seconds, you can see the output "callback is called".
Question one: If you want to write a callback function for settimeout, call output "Hello World" after 1 seconds, how to write it, try it.
Small extension: the SetTimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds. Syntax: SetTimeout (CODE,MILLISEC);
3. Standard callback function
The callback function format in node. JS is conventional, it has two parameters, the first argument is err, the second parameter is data, and as the name implies, err is the error message, and data is the one returned by the following example:
function (Err,data) {}
In order to develop a good coding habit, the callback function format will be used in future course examples or exercises.
Question two: Try to write a callback function yourself!
Small expansion: A callback function, or callback, that is not called directly by the implementing party of the function, but is called by another party when a particular event or condition occurs, and is used to respond to the event or condition.
4. Get the module
In order to support rapid development, the node platform provides a large number of modules, encapsulating the different functions, then we will get the desired module, we come to introduce. In node, we can use the Require function with the following syntax:
Require ("module");
How, is not very simple very convenient, through the Require function we can obtain the corresponding module and then use its arbitrary function.
5. Using the module
The OS module provides some basic information about the operating system, and some of its common methods are as follows:
var OS = require ("OS"); var result = Os.platform (); View operating system Platform //os.release (); View operating system version //os.type (); View operating system name //os.arch (); View operating system CPU Architecture console.log (result);
Looking at the example above, try the version number of the Console.log output server operating system.
Small expansion: module Description: Can be divided into the core module and file module, the core module is compiled into binary code, the reference is only a require notation, the file module refers to a JS file, a JSON file or a. node file.
6. Summary of Courses
This section describes some of the most basic uses of node. js to lay the groundwork for future courses.
In addition, some API information is recommended for learning purposes.
http://nodejs.cn/apihttp://nodeapi.ucdok.com/#/api/
Node. JS Learning First day