Reading notes: Nodejs reference documents

Source: Internet
Author: User
Tags emit readable

>> Buffer
The buffer object is a global object
Buffer supported encoding: ASCII, UTF8, base64, binary
New Buffer (size)
New Buffer (Array)
New Buffer (str, encoding= ' UTF8 ')

Buffer.write (str,offset=0, encoding= ' UTF8 ')
Buffer.tostring (encoding, start=0, end=buffer.length);

Buffer[index]

Buffer.bytelength (String, encoding= ' UTF8 ')
Buffer.length//Memory size bytes allocated to the Buffer object

Buffer.copy (Targetbuffer, Targetstart, Sourcestart, Sourceend=buffer.length)
Buffer.slice (start, end);//Returns a buffer object that references the same memory, noting that it differs from Str.slice ()


>> Eventemitter
All objects that are capable of triggering events are instances of Events.eventemitter.
Newlistener event is triggered when a new event listener is registered
An error event is triggered, and if not captured, the call stack is output and the app is exited

* Event:newlistener
* Event:error

* Emitter.on (event, listener); Add 1 listeners to a listener array
Such as:
Server.on (' Stream ', function () {
Console.log (' someone connected ');
});

* Emitter.removelistener (event, listener); Removes the specified listener from the specified listeners array (the Listener array resizes the index splice)
Such as:
var callback = function (stream) {Console.log (' Someone connected! ')};

Server.on (' stream ', callback);
//...
Server.removelistener (' stream ', callback);

* Emitter.removealllisteners (Event)

* Emitter.listeners (event); Returns the listener array for the specified event

* Emitter.emit (event, [Arg1], [arg2],...); Triggering events

>> streams
Stream is an abstract interface, and many objects in node implement this interface (such as request, stdout), and the stream object is an instance of Eventemitter.
Stream can be read-only, write-only, readable and writable

> Readable stream read-only stream
Methods, properties, and events for read-only streams

* Event:data
Stream.on (data, function (data) {
Data:buffer or String (if Setencoding () is called)
});

* Event:end
triggered when the stream encounters a fin in EOF or TCP, indicating no more data coming in
Stream.on (' End ', function () {});

* Event:error
Stream.on (' Error ', function (exception) {});

* Event:close
triggered when the internal file descriptor is closed
Stream.on (' Close ', function () {});

* EVENT:FD
triggered when the data stream receives the file descriptor information (data flow information for a file consists of 2 parts: File descriptor information and file data information) UNIX stream
Stream.on (' fd ', function (FD) {});

* stream.readable

* stream.setencoding (encoding);
The Set data event returns a string instead of a buffer object, encoded by: ASCII, UTF8, base64

* Stream.pause (); Pause triggering Data Event

* Stream.destroy ();
Close the internal file descriptor so that the stream no longer triggers any events

> Writable Stream writable Stream
Methods, properties, and events for writable streams:

* Event:drain
triggered when a write method is called and returns false, indicating that the stream can be safely written again
Stream.on (' Drain ', function () {});

* Event:error
Stream.on (' Error ', function (exception) {});

* Event:close
triggered when the underlying file descriptor has been terminated
Stream.on (' Close ', function () {});

* stream.writable

* Stream.Write (string, encoding= ' UTF8 ', [FD]);
Encodes a string with the specified encoding to write to the stream, returns True if the string is successfully flushed to the kernel buffer, otherwise returns false, and the data is written in the future.
Drain event notifies when kernel buffers are empty

* Stream.Write (buffer); Ditto

* Stream.end ();
To terminate a stream with EOF or fin

* Stream.end (string, encoding); ~ ~ Equivalent to Stream.Write (string, encoding), Stream.end ()

* Stream.end (buffer); Ditto

* Stream.destroy ();
Terminates the underlying file descriptor, after which the stream does not emit any events

>> Globals Objects Global objects

* Global
Global Namespace Object ~ ~ Similar to window in a browser environment?

* Process Object

* require () load dependent module

* Require.paths
An array that holds the Require function search path and adds a custom path to it

* __filename
The absolute path of the currently executing script

* __dirname
The folder that is currently executing the script

* Module
Reference to the current module, module.exports and exports point to the same reference


> Process
The process object is an instance of Eventemitter

* Event:exit
Triggered when the process exits, a good place to check the status of the module (e.g. unit tests)
Process.on (' Exiti ', function () {});
Such as:
Process.on (' Exit ', function () {
Process.nexttick (function () {//Next event loop
Console.log (' This is not run ');
});
Console.log (' About to exit ');
});

* Event:uncaughtexception not caught exception
Process.on (' Uncaughtexception ', function (Error) {});
Such as:

Reading notes: Nodejs reference documents

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.