Nodejs Quick Start (4) - NodeJS I/O

1. Understand I/O

    I/O (input / output), that is, input and output, input and output on the computer such as audio recording is the input of business, listening to music is the output of sound, which can be understood as read and write operations on the server.

2. Concurrency

    It means that there are several programs in a period of time that are in the period from running to completion, and these programs are all running on the same processor, but only one program is running on the processor at any point in time. Nodejs is a single-line execution code, using asynchronous instead of multi-threading.

3. Synchronous and asynchronous

    Synchronization, as shown in the figure below, the first orderer does not finish ordering, and the subsequent orderers can only queue up. In the program, the current code does not finish running, and the next code cannot be run.

    

    Asynchronous, as shown in the figure below, a person goes to order a meal. After ordering what he wants, he waits in the rest area. After the meal is prepared, it will be delivered after delivery or call to the pick-up station to pick up the real thing. This way the order will not be blocked, but it may not be on a first-come, first-served basis.


    The difference between synchronous and asynchronous: asynchronous will not block the code behind, synchronous will block the code behind.

    Who executes the synchronous code and the asynchronous code in the same piece of code first?

setTimeout(function(){
		console.log(4);
	},1000);

	console.log(1);
	console.log(2);
	console.log(3);

Result: 1 2 3 4

 The answer can be known, the synchronous code is executed first, and the asynchronous code is executed.

Execution schematic


callback function callback simulation

var fn = function(callback){
	setTimeout(function(){
		var food = "Fried Chicken Thighs";
		callback(food);
	},1000);

	console.log("Meals");
}

fn(function(data){
	console.log(data);
});

Common asynchronous operations

Asynchronous non-I/O setTimeout、setInterval
Asynchronous I/O     network operation http file operation fs

4. Use of __dirname and path variables

 files in the module directory

1.js

var fr = require('./module/2.js');

fr('test.txt',function(data){
	console.log(data);
});

When the code in 2.js is

var fs = require('fs');

var fr = function(file,callback){
	fs.readFile(file,'utf8',(err,data)=>{
		callback(data);
	});
}

module.exports = fr;

The output result is the content in the test.txt file in the same directory of the module.

However, what I want to output is the content of the test.txt file in the module directory. The code in 2.js needs to introduce __dirname

var fs = require('fs');

var fr = function(file,callback){
	fs.readFile(__dirname + file,'utf8',(err,data)=>{
		callback(data);
	});
}

module.exports = fr;

file is a parameter, and there is no restriction on how the parameter is written, but if the value of file is Ctrip "./test.txt", the test.txt file will not be accessed, and the path variable can be used here to filter multi-user input.

The method path.join() at the core of the path variable, in the above code, modify the appeal code to

var fs = require('fs');

var fr = function(file,callback){
	fs.readFile(path.join(__dirname,file),'utf8',(err,data)=>{
		callback(data);
	});
}

module.exports = fr;

It can play a better compatibility effect, and can also be accessed by passing in "./test.txt".

5. Exception handling

    Exception: a situation that the computer cannot handle

In the following code, abc.js is a file that does not exist, execute the code

var fs = require('fs');

fs.readFile('abc.js','utf8',function(err,data){
	console.log(err);
	console.log(123);
});


 Can see console.log(123); can print. Node's async methods do not handle exceptions. 
 
var fs = require('fs');

fs.readFile('abc.js','utf8');
console.log(123);

Results of the

 Nodejs handles exceptions to synchronous code.

The difference between synchronous and asynchronous exception handling. Use try{} catche(e){} for the following exception handling.

This is a piece of asynchronous code exception handling.

var fs = require('fs');

try{
	fs.readFile('abc.js','utf8',function(err,data){
		if(err) throw err;
	});
}catch(e){
	console.log(e);
}

This way of writing cannot handle exceptions, because the try catch code has been processed and executed, and the asynchronous code operation is executed.

Whether it is asynchronous or synchronous code, only synchronous processing can be performed, and the processing is as follows

fs.readFile('abc.js','utf8',function(err,data){
		try{
				if(err) throw err;
				
		}catch(e){
			console.log(e);
		}
	});

result



    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325622249&siteId=291194637