First of all, Node.js as a JavaScript platform, it uses event-driven and asynchronous programming, through event registration and asynchronous functions, developers can improve resource utilization, server performance can also be improved. Second, for the front end people, Node.js as a running platform for JS, we can write system-level or server-side JavaScript code to Node.js to execute, let us front-end people can also be used in the background, in contrast, browser-side JavaScript code in the runtime will be subject to a variety of security restrictions, the operation of the customer system has , while Node.js is a comprehensive background runtime that provides JavaScript with many of the features that many other languages can achieve.
The following return to the topic, first of all to introduce blocking calls, the specific content please look down.
1. Blocking calls (after reading the file and then performing the subsequent operation)
var fs = require ("FS");
var data = Fs.readfilesync ('/fs.txt ');
Console.log (Data.tostring ());
Console.log ("End of program execution!");
Output results:
"File Contents"
"End of program execution!" ”
2. Non-blocking calls (read files and other actions synchronously)
var fs = require ("FS");
Fs.readfile ('/fs.txt ', function (err,data) {
if (err) return Console.error (err);
Console.log (data.tostring ()
); Console.log ("End of program execution!");
Output results:
"End of program execution!" ”
"File Contents"
The above content is small to introduce the Node.js callback function block call and non-blocking call all content, I hope you like.