标签:
Node.js v4.4.7 Documentation(官方文档)
Prior to the introduction of TypedArray
in ECMAScript 2015 (ES6), the JavaScript language had no mechanism for reading or manipulating streams of binary data(二进制数据). The Buffer
class was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.
This module has utilities for URL resolution(url解析) and parsing. Call require(‘url‘)
to use it.
url.parse() 返回对象
Parsed URL objects have some or all of the following fields, depending on whether or not they exist in the URL string. Any parts that are not in the URL string will not be in the parsed object.
Url {
protocol: ‘http:‘, // 协议
slashes: true, // 是否有斜线
auth: null,
host: ‘jingyan.baidu.com‘, //主机域名 including port information(包括端口)
port: null, //端口
hostname: ‘jingyan.baidu.com‘, //主机域名
hash: null, //哈希,锚
search: null,
query: null,
pathname: ‘/article/93f9803fd3a4dde0e46f55f5.html‘, //
path: ‘/article/93f9803fd3a4dde0e46f55f5.html‘, // Concatenation of pathname
and search
href: ‘http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html‘ // The full URL
}
Take a parsed URL object, and return a formatted URL string.
返回一个url字符串
url.resolve(‘/one/two/three‘, ‘four‘) // ‘/one/two/four‘
url.resolve(‘http://example.com/‘, ‘/one‘) // ‘http://example.com/one‘
url.resolve(‘http://example.com/one‘, ‘/two‘) // ‘http://example.com/two‘
Take a URL string, and return an object.
Pass true
as the second argument to also parse the query string using the querystring
module(Node.js内置的Query String模块). If true
then the query
property will always be assigned an object, and the search
property will always be a (possibly empty) string. If false
then the query
property will not be parsed or decoded. Defaults to false
.
This module provides utilities for dealing with query strings(查询字符串处理)
Serialize an object to a query string. 把一个对象序列化为一个查询字符串
Optionally override the default separator (‘&‘
) and assignment (‘=‘
) characters(默认的分隔符是 & =).
Deserialize(反序列化) a query string to an object. Optionally override the default separator (‘&‘
) and assignment (‘=‘
) characters.
querystring.escape 转义
querystring.unescape 反转义
File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require(‘fs‘)
. All the methods have asynchronous and synchronous forms(都有异步和同步两种格式).
file
<String> filenameoptions
<Object> | <String>
encoding
<String> | <Null> default = null
flag
<String> default = ‘r‘
callback
<Function>If no encoding is specified, then the raw buffer is returned.
To use the HTTP server and client(服务器端和客户端), one must require(‘http‘)
.
The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses--the user is able to stream data.
options
can be an object or a string(对象或字符串). If options
is a string, it is automatically parsed with url.parse()
.
The optional callback
parameter will be added as a one time listener for the ‘response‘
event.
Since most requests are GET requests without bodies, Node.js provides this convenience method. The only difference(与request的区别) between this method and http.request()
is that it sets the method to GET and calls req.end()
automatically.
Much of the Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") periodically emit named events that cause Function objects ("listeners") to be called.
All objects that emit events are instances of the EventEmitter
class(是EventEmitter的实例). These objects expose an eventEmitter.on()
function(这些对象(emitter)暴露出来.on()方法,允许添加一些listeners(Function)) that allows one or more Functions to be attached to named events emitted by the object.
The eventEmitter.on()
method is used to register listeners(注册监听器), while the eventEmitter.emit()
method is used to trigger the event(激发事件).
Event模块(events.EventEmitter)是一个简单的事件监听器模式的实现。具有addListener/on,once,removeListener,removeAllListeners,emit等基本的事件监听模式的方法实现。
var EventEmitter = require(‘events‘);
Alias for emitter.on(eventName, listener)
.
Synchronously calls each of the listeners registered for the event named eventName
, in the order they were registered, passing the supplied arguments to each.
Returns true
if the event had listeners(如果有监听), false
otherwise.
eventName
<Value> The name of the event being listened forReturns the number of listeners listening to the event named eventName
.
Returns a copy of the array of listeners for the event named eventName
.
标签:
原文地址:http://www.cnblogs.com/oneplace/p/5661245.html