1 Buffer
Buffer is a tool provided by node to process binary byte streams. The raw data is stored in the buffer object. to convert the data with a string, you must specify the string encoding. Currently, node supports the following encodings: ASCII, utf8, utf16le, base64, and hex. Binary may be discarded by node in the future.
The following describes the methods and attributes of the buffer class.
New buffer (size) New buffer (array) New buffer (STR, [encoding]) to construct a buffer
Tostring ([encoding], [start], [end]) converts the buffer into a string
Write (STR, [encoding]) writes STR to buffer
Buf [Index] accesses byte content based on location
Bytelength (STR, [ENC]) returns the length of STR in bytes.
Concat (list) connects the buffer in the list and returns
Copy (DST, [DST-start], [Src-start], [Src-end]) to copy the specified data to the specified location of the destination buffer, someone suggested not to use the + on the Buf, but to use copy to increase the speed.
Slice ([start], [end])
Readuint8 (offset) reads one byte from the buffer, similar to readuint16le readuint16be, readuint32le/be
Writeuint8 (value, offset) writes data to the buffer. The corresponding interfaces are the same as those of the read series.
In network programming, there is a distinction between the host sequence and the network sequence. Le adopts the small-end reading method and is the host sequence on the small-end machine. Be adopts the big-end reading method and uses the network sequence on the small-end machine.
Fill buff with fill (value, [offset], [end ])
Length
2 HTTP
The HTTP module is complex, that is, server support and client support.
2.1 Components supported by Server
2.1.1 HTTP. Server
This module provides a class for creating an HTTP server. It supports the following methods:
Listen (port, [host]. [backlog], [callback])
Listen (path, [callback])
Listen (handle, [callback])
Close ([callback])
You can handle the following events:
Connection function (socket) {} triggered every time a TCP connection is established
Triggered when the close function (0 {} service is closed
Connect function (req, Sock, head) {} triggered when the HTTP request is a connect method
Clienterror function (excp) {} triggered when a client error occurs
Request function (req, resp) {} is triggered when a request arrives. req is an HTTP. serverrequest instance, and resp is an HTTP. serverresponse instance.
2.1.2 serverrequest
It is created inside the HTTP module and is implemented by the readable stream interface. The following events are supported.
Data function (chunk) {} is triggered when data occurs in a stream. If the stream is set to chunk encoded as a string, otherwise it is a buffer.
End function () {} triggered when end () is called on the stream
Close function () {} is triggered when the underlying connection is closed.
Attribute description
Method HTTP method, read-only
URL request URL string, only the path in the first line
HTTP header in headers request
Connection net. Socket object
Method description
Setencoding ()
Pause ()/resume ()
2.1.3 serverresponse
If the writable stream interface is implemented, the following events are triggered.
Close ()
Method description:
Writehead (Code, [code string], [headers])
Setheader (name, value)
Removeheader (name)
Write (chunk, [encoding])
End ([data], [encding])
Attribute description
Statuscode
2.2 client components
2.2.1 HTTP. Request (option, callback)
Used to send an HTTP request, option = {Host, hostname, port, lcoaladdress, socketpath, method, path, headers, auth, Agent}, callback = function (response ). returns an HTTP. clientrequest object.
HTTP. Get (option, callback) is used to support GET request callback = function (response)
2.2.2 HTTP. clientrequest
Supported events:
Response Function (RESP ){}
Socket function (socket ){}
Connect function (resp, Sock, head ){}
Supported Methods
Write (data, [encoding])
End ([data], [encoding])
Abort ()
SetTimeout (timeo, [callback])
Setnodelay ()
Setsocketkeepalive ()
2.2.3 HTTP. clientresponse
Event
Data
End
Close
Attribute
Statuscode
Httpversion
Headers
Method
Setencoding ()
Pause ()
Resume ()
3 dgram/udp Server
The dgram module allows us to quickly build a UDP server or client.
Creatsocket (type, [callback]) returns a UDP socket, which will be used in subsequent operations.
For socket classes
BIND (port, [address]) bind the port and address for the socket
Send (BUF, start, length, port, address, [callback]) send data to the destination address through this socket
Close () Close socket
Address () gets the local address of the socket, {port, address, Protocol}
Setttl: sets the TTL attribute of an IP address.
Setbroadcast (FLAG) is set as multicast attribute
Setmulticastttl (TTL) sets the IP multicast TTL attribute
Setmulticastloopback (FLAG) is used to set or clear the local loop to receive multicast functions.
Addmembership (multiaddr, [interface]) is added to multicast groups at the specified interface.
Dropmembership (multiaddr, [interface ])
Socket events:
Message: function (MSG, raddr) {} triggered when UDP receives a message
Error: function (ERR) {} triggered when an exception occurs
Close: function (){}
The following is a UDP server code. The client sends hello to the server and the server returns world.
var udpServer = require('dgram');var main = function(){ socket = udpServer.createSocket('udp4'); socket.bind(2048); //var local = socket.address(); socket.on('error',function(err){ console.log('error has happened ',err); }); socket.on('message',function(data,addr){ console.log('recvfrom addr ',addr); var rbuf = new Buffer('world','utf-8'); }); var rbuf = new Buffer('hello'); var opt = {ip:'192.168.1.3',port:8080}; socket.send(data,0,data.length,opt.port,opt.ip);};main();
4. net
The. NET module allows us to quickly build TCP server and client modules. The specific modules are not described in detail here, similar to the HTTP module.
5 dns
This module is relatively simple. A series of functions are exported to complete DNS request and Record Parsing.
Lookup (domain, 'Family ', callback) callback (ERR, address, family ){}
Resolve (domain, [rrtype], callback) callback (ERR, address) {} type includes: A, AAAA, MX, txt, PTR, SRV, NS, cname
var dns = require('dns');var dom = process.argv[2];/*dns.lookup(dom,function(err,addr,family){ console.log('1',err,addr,family);});*/var types = ['A','AAAA','MX','TXT','SRV','PTR','NS','CNAME'];var handler = function(type){ try{ dns.resolve(dom,type,function(err,address){ if(!err) console.log(type,address); else console.log(type,err); }); }catch(exp) { console.log('unsupport method'); }}types.forEach(handler);