Node.js 常用 API
Node.js v6.11.2 Documentation(官方文档)
Buffer
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.
URL
This module has utilities for URL resolution(url解析) and parsing. Call require('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 pathname and 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 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.
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 do require('fs'). All the methods have asynchronous and synchronous forms(都有异步和同步两种格式).
fs.readFile(file[, options], callback)
file<String> filenameoptions<Object> | <String>encoding<String> | <Null> default =nullflag<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 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.
http.request(options[, callback])
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.
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 and http.request() is that it sets the method to GET and calls req.end() automatically.
EVENT
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');
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 true if the event had listeners(如果有监听), false otherwise.
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
The path module provides utilities(模块提供了关于文件路径和目录路径操作的工具方法) for working with file and directory paths.
Node.js 常用 API的更多相关文章
- AngularJS 授权 + Node.js REST api
作者好屌啊,我不懂的他全都懂. Authentication with AngularJS and a Node.js REST api 几个月前,我开始觉得 AngularJS 好像好牛逼的样子,于 ...
- Node.js RESTful API
什么是REST架构? REST表示代表性状态传输.REST是一种基于Web标准的架构,并使用HTTP协议. 它都是围绕着资源,其中每一个组件是资源和一个资源是由一个共同的接口使用HTTP的标准方法获得 ...
- Node.js 常用工具
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherit ...
- Practical Node.js (2018版) 第8章:Building Node.js REST API Servers
Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...
- Node.js 常用工具util包
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.isError(obj); util.is ...
- 十个书写Node.js REST API的最佳实践(上)
收录待用,修改转载已取得腾讯云授权 原文:10 Best Practices for Writing Node.js REST APIs 我们会通过本文介绍下书写Node.js REST API的最佳 ...
- 十个书写Node.js REST API的最佳实践(下)
收录待用,修改转载已取得腾讯云授权 5. 对你的Node.js REST API进行黑盒测试 测试你的REST API最好的方法之一就是把它们当成黑盒对待. 黑盒测试是一种测试方法,通过这种方法无需知 ...
- 编写 Node.js Rest API 的 10 个最佳实践
Node.js 除了用来编写 WEB 应用之外,还可以用来编写 API 服务,我们在本文中会介绍编写 Node.js Rest API 的最佳实践,包括如何命名路由.进行认证和测试等话题,内容摘要如下 ...
- 使用Node.js原生API写一个web服务器
Node.js是JavaScript基础上发展起来的语言,所以前端开发者应该天生就会一点.一般我们会用它来做CLI工具或者Web服务器,做Web服务器也有很多成熟的框架,比如Express和Koa.但 ...
随机推荐
- Mac休眠之后唤醒时无法使用鼠标
Mac休眠之后唤醒时,无法使用鼠标键盘,无法输入密码登录,只能重启. 尝试升级系统,问题依旧,最后在Google的帮助下,问题解决. 解决办法:系统偏好设置->节能->取消硬盘休眠 参考: ...
- win10开启 linux Bash命令(win10内置了linux系统支持)
win10开启 Ubuntu linux Bash命令(win10内置了linux系统支持) 第一步: 先在设置→更新和安全→针对开发人员中选择"开发人员模式",点击后会下载&qu ...
- 如何快速将文本中的tab更换成逗号(图文详解)
不多说,直接上干货! 现有一份数据如下. 下载日志数据并分析 到搜狗实验室下载用户查询日志 1) 介绍 搜索引擎查询日志库设计为包括约1个月(2008年6月)Sogou搜索引擎部分网页查询需求及用户点 ...
- 翻译:WebAssembly简介:我们为什么要关心这个技术?
原文: https://tomassetti.me/introduction-to-webassembly/ WebAssembly简介:我们为什么要关心这个技术? 在对抗js的伟大战斗中有 ...
- Go语言学习笔记二: 变量
Go语言学习笔记二: 变量 今天又学了一招如何查看go的版本的命令:go version.另外上一个笔记中的代码还可以使用go run hello.go来运行,只是这种方式不会生成exe文件. 定义变 ...
- web开发之缓存
以数据为驱动的web站点,当访问量增大后,由于频繁的从DB中读取数据,使得DB服务器的压力大增,从而影响系统的性能.为了缓解这种来自于大访问量的频繁读取DB的压力,我们可以把一些数据缓存起来,当请求过 ...
- 我和ARM的那些事儿3 beep之旅,最详细的ARM裸机工程设置
前言 在博客园里面我搜索了老半天,找相应的mini2440的裸机开发的程序,让我挺失望的就是居然没有人对做arm这块做详细的解答,到底如何去做,到底如何去配置,都不清楚,让我很纠结,那么我花了近一星期 ...
- 关于svn插件突然失效问题
这个分享一下 安装 MyBatisGenerator 插件 之后,svn失效,删掉mybatis 后,svn就恢复正常...这怎么割 一翻折腾无效,后来发现 MyBatisGenerator 和 ...
- iOS开源项目周报0209
由OpenDigg 出品的iOS开源项目周报第七期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.Hedwig 向 ...
- [转]微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
本文转自:http://blog.csdn.net/qq_31383345/article/details/53014610 今天遇到微信小程序的用户头像设置功能,做笔记. 先上gif: 再上代码: ...