Node.js 常用 API
2016-07-12 19:10
447 查看
Node.js v6.11.2 Documentation(官方文档)
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
href: 'http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html' // The full URL
}
返回一个url字符串
Pass
querystring.stringify(obj[, sep][, eq][, options])
Serialize an object to a query string. 把一个对象序列化为一个查询字符串
Optionally override the default separator (
querystring.unescape 反转义
fs.readFile(file[, options], callback)
If no encoding is specified, then the raw buffer is returned.
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.
The optional
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
The
Event模块(events.EventEmitter)是一个简单的事件监听器模式的实现。具有addListener/on,once,removeListener,removeAllListeners,emit等基本的事件监听模式的方法实现。
emitter.addListener(eventName, listener)
Alias for
emitter.emit(eventName[, arg1][, arg2][, ...])
Synchronously calls each of the listeners registered for the event named
Returns
emitter.listenerCount(eventName)
Returns the number of listeners listening to the event named
emitter.listeners(eventName)
Returns a copy of the array of listeners for the event named
Buffer
Prior to the introduction ofTypedArrayin ECMAScript 2015 (ES6), the JavaScript language had no mechanism for reading or manipulating streams of binary data(二进制数据). The
Bufferclass 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.
URL
This module has utilities for URL resolution(url解析) and parsing. Callrequire('url')to use it.
URL(全球资源定位器(Uniform Resource Locator))
URL Parsing
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
pathnameand
search
href: 'http://jingyan.baidu.com/article/93f9803fd3a4dde0e46f55f5.html' // The full URL
}
url.format(urlObj)
Take a parsed URL object, and return a formatted URL string.返回一个url字符串
url.resolve(from, to)
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'
url.parse(urlStr[, parseQueryString][, slashesDenoteHost])
Take a URL string, and return an object.Pass
trueas the second argument to also parse the query string using the
querystringmodule(Node.js内置的Query String模块). If
truethen the
queryproperty will always be assigned an object, and the
searchproperty will always be a (possibly empty) string. If
falsethen the
queryproperty will not be parsed or decoded. Defaults to
false.
Query String
This module provides utilities for dealing with query strings(查询字符串处理)querystring.stringify(obj[, sep][, eq][, options])
Serialize an object to a query string. 把一个对象序列化为一个查询字符串Optionally override the default separator (
'&') and assignment (
'=') characters(默认的分隔符是 & =).
querystring.parse(str[, sep][, eq][, options])
Deserialize(反序列化) a query string to an object. Optionally override the default separator ('&') and assignment (
'=') characters.
querystring.escape querystring.unescape
querystring.escape 转义querystring.unescape 反转义
File System
File I/O is provided by simple wrappers around standard POSIX functions. To use this module dorequire('fs'). All the methods have asynchronous and synchronous forms(都有异步和同步两种格式).
fs.readFile(file[, options], callback)
file<String> filename
options<Object> | <String>
encoding<String> | <Null> default =
null
flag<String> default =
'r'
callback<Function>
If no encoding is specified, then the raw buffer is returned.
HTTP
To use the HTTP server and client(服务器端和客户端), one mustrequire('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.
http.request(options[, callback])
optionscan be an object or a string(对象或字符串). If
optionsis a string, it is automatically parsed with
url.parse().
The optional
callbackparameter will be added as a one time listener for the
'response'event.
http.get(options[, callback])
Since most requests are GET requests without bodies, Node.js provides this convenience method. The only difference(与request的区别) between this method andhttp.request()is that it sets the method to GET and calls
req.end()automatically.
EVENT
深入浅出Node.js(四):Node.js的事件机制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
EventEmitterclass(是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');
emitter.addListener(eventName, listener)
Alias for emitter.on(eventName, listener).
emitter.emit(eventName[, arg1][, arg2][, ...])
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
trueif the event had listeners(如果有监听),
falseotherwise.
emitter.listenerCount(eventName)
eventName<Value> The name of the event being listened for
Returns the number of listeners listening to the event named
eventName.
emitter.listeners(eventName)
Returns a copy of the array of listeners for the event named eventName.
Path
Thepathmodule provides utilities(模块提供了关于文件路径和目录路径操作的工具方法) for working with file and directory paths.
相关文章推荐
- NODEjs自动重启服务
- Leetcode 24. Swap Nodes in Pairs (Easy) (cpp)
- node.js windows下npm配置全局模块express.
- 对express中next函数的一些理解
- Build a RESTful API Using Node and Express 4
- Node.js-中文分词【1】-node-segment
- node.js安装
- Leetcode 237. Delete Node in a Linked List (Easy) (cpp)
- node -- all
- nodejs 常用 index
- 通过sed和awk获取集群最新挂掉的DataNode信息
- nodejs:express API之res.locals
- C#操作XML文档(XmlDocument、XmlNode、XmlAttribute、SelectSingleNode、SelectNodes、XmlNodeList)
- 116. Populating Next Right Pointers in Each Node && 117. Populating Next Right Pointers in Each Node II
- Leetcode 19. Remove Nth Node From End of List (Easy) (cpp)
- nodejs下的图形处理库gm在windows环境的安装
- NODEJS mongoose 增删改查
- hadoop节点nodemanager启动失败原因小结~
- 关于nodejs的npm命令无反应的解决方案
- LeetCode - 24. Swap Nodes in Pairs