Noejs Learning Notes

The direct embodiment of Node.js asynchronous programming is callback.

Asynchronous programming relies on callbacks to achieve, but it can not be said that the program will be asynchronous after using callbacks.
Callback functions are called when the task is completed. Node uses a large number of callback functions. All of Node's API s support callback functions.
For example, we can read the file and execute other commands at the same time. After reading the file, we return the content of the file as a parameter of the callback function.
This way, there is no blocking or waiting for file I/O operations when the code is executed. This greatly improves the performance of Node.js and can handle a large number of concurrent requests.

Blocking code example:
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("End of Program Execution!");
Non-blocking code instance:
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
    if (err) return console.error(err);
    console.log(data.toString());
});
console.log("End of Program Execution!");
Event-driven:
// Introducing Evets Module
var events = require('events');
// Create EvetEmitter objects
var eventEmitter = new events.EventEmitter();

// Create event handlers
var connectHandler = function connected() {
   console.log('The connection was successful.');

   // Triggering data_receive event 
   eventEmitter.emit('data_received');
}

// Binding connection event handler
eventEmitter.on('connection', connectHandler);

// Binding data_receive events with anonymous functions
eventEmitter.on('data_received', function(){
   console.log('The data was received successfully.');
});

// Triggering connection events 
eventEmitter.emit('connection');

console.log("The program has been executed.");
In Node applications, functions that perform asynchronous operations take the callback function as the last parameter and the callback function receives the wrong object as the first parameter.

Node.js EventEmitter

All asynchronous I/O operations in Node.js send an event to the event queue at completion.
Many objects in Node.js distribute events: a net.Server object distributes an event every time a new connection occurs.
An fs.readStream object emits an event when the file is opened.
All of these objects that generate events are instances of events.EventEmitter.

Node.js Buffer
var buf = new Buffer(10);
var buf = new Buffer([10, 20, 30, 40, 50]);
var buf = new Buffer("www.baidu.com", "utf-8");
buf.write(string[, offset[, length]][, encoding]);

String - A string written to a buffer.
offset - The index value that the buffer begins to write is 0 by default.
length - The number of bytes written by default is buffer.
Encoding - The encoding used. The default is `utf8'.
Read buffer:
buf.toString([encoding[, start[, end]]])
parameter
Encoding - The encoding used. The default is `utf8'.
Start - Specifies the index location to start reading, defaulting to 0.
End - End position, default to the end of the buffer.
Return value
Decode the buffer data and return the string using the specified encoding.
Example

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97;
}

console.log( buf.toString('ascii'));       // Output: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5));   // Output: abcde
console.log( buf.toString('utf8',0,5));    // Output: abcde
console.log( buf.toString(undefined,0,5)); // Use'utf8'encoding and output: abcde

Keywords: encoding Programming ascii

Added by capetonian on Sat, 13 Jul 2019 23:37:54 +0300