博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Node】常用基础 API 整理
阅读量:6989 次
发布时间:2019-06-27

本文共 8187 字,大约阅读时间需要 27 分钟。

一、Debug 调试方法

Node 的调试方法有很多,主要分为安装 node-inspect 包调试、用 Chrome DevTools 调试和 IDE 调试,可以在官网的 查看安装方法。

下面介绍使用 Chrome DevTools 调试的方法,首先安装 ,打开 Inspect 入口页面 chrome://inspect

写一个简单 debug.js 测试文件:

// apiTest/debug.js console.log("this is debug test")function test () {  console.log("hello world")}test()复制代码

使用node --inspect-brk来启动脚本,-brk相当于在程序入口前加一个断点,使得程序会在执行前停下来

$ node --inspect-brk apiTest/debug.js Debugger listening on ws://127.0.0.1:9229/44b5d11e-3261-4090-a18c-2d811486fd0aFor help, see: https://nodejs.org/en/docs/inspector复制代码

在 chrome://inspect 中设置监听端口 9229(默认),就可以看到可以 debug 的页面:

(function (exports, require, module, __filename, __dirname) { console.log("this is debug test")function test () {  console.log("hello world")}test()});复制代码

如果我们使用node --inspect来启动脚本,那整个代码直接运行到代码结尾,无法进行调试,但此时 Node 还进程没有结束,所以可以在 查询 devtoolsFrontendUrl ,复制此 Url 到 Chrome 上进行调试。

看到使用 Chrome DevTools 的调试方法还是比较复杂的,一些 IDE 都支持直接断点调试,推荐WebStorm、VScode。

二、全局变量

在 Node 中常用的全局方法有 CommonJS、Buffer、process、console、timer 等,这些方法不需要 require引入 API 就可以直接使用。

如果希望有属性或方法可以*“全局使用”*,那就将它挂载在 Node 的global对象上:

global.gNum = 300console.log(gNum); // 300复制代码

在 Node 中所有模块都可以使用这些全局变量,以下就介绍 Node 中的全局变量

2.1 CommonJS 模块

Node CommonJS 模块规范根据实现了moduleexportsrequire模块机制。Node 对每个文件都被进行了模块封装,每个模块有自己的作用域,如在 debug 时看到的:

(function (exports, require, module, __filename, __dirname) { 	// some code});复制代码

模块机制中的 __dirname__filenameexportsmodulerequire()这些变量虽然看起来是全局的,但其实它们仅存在于模块范围。需要注意的几点是:

  • 模块内部module变量代表模块本身
  • 模块提供require()方法引入外部模块到当前的上下文中
  • module.exports属性代表模块对外接口,默认的快捷方式exports

简单的使用方式如下:

/* common_exports.js */exports.num = 100  exports.obj = {  a : 200}exports = {  count : 300}/* common_require.js */const mod = require('./common_exports')console.log(mod) // { num: 100, obj: { a: 200 } }console.log(mod.count)  // undefined复制代码

注意到上例中的mod.countundefined,这是因为exports只是module.exports的引用,可以给exports添加属性,但不能修改exports的指向。

更深入的了解模块机制可以看

2.2 process 进程对象

process 包含了进程相关的属性和方法,Node 的 中的内容特别多,列举几个常用方法。

Node 进程启动时传递的参数都在 process.arg数组中:

// process.jsconst {argv , execPath} = processargv.forEach((val, index) => {  console.log(`${index}: ${val}`)})console.log(execPath)复制代码

可以在执行 process.js 时传递其他参数,这些参数都会保存在argv中:

$ node apiTest/process.js one=1 --inspect --version0: /usr/local/bin/node1: /Users/mobike/Documents/webProjects/testNode/apiTest/process.js2: one=13: --inspect4: --version/usr/local/bin/node复制代码

process.argv第一个参数就是 process.execPath ,即调用执行程序 Node 的路径,第二个参数时被执行的 JS 文件路径,剩下的就是自定义参数。


process.env是包含运行环境各种参数的对象,可以直接输出env查看所有参数信息,也可以输出某个属性:

const env = process.envconsole.log(env.PATH) // /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Documents/webProjects/testNode/node_modules/.binconsole.log(env.SHELL) // /bin/zsh复制代码

在 webpack 打包过程中常用process.env.NODE_ENV判断生产环境或开发环境,process.env是没有NODE_ENV这个属性的,你可以在系统环境变量中配置,也可以在项目程序直接设置process.env.NODE_ENV=‘dev’


process.cwd()方法返回 Node.js 进程的当前工作目录,和 Linus 命令$ pwd功能一样:

// process.jsconsole.log(process.cwd()) // /Users/Documents/webProjects/testNode复制代码
$ node process.js/Users/WebstormProjects/testNode$ pwd/Users/WebstormProjects/testNode复制代码

2.3 Timers 异步

Node 中的计时器方法与 Web 浏览器中的JS 计时器类似,但内部实现是基于 Node 的 。Node 中的计时器有setImmediate()setTimeout()setInterval()

在 Node 中有一个轻量级的process.nextTick()异步方法,它是在当前事件队列结束时调用, setImmediate()是当前 Node Event Loop 结束时立即执行,那执行顺序有什么区别呢?

下面举例说明process.nextTick(fn)setImmediate(fn)setTimeout(fn,0)之间的区别:

// timer.js setImmediate(()=>{  console.log("setImmediate")});setTimeout(()=>{  console.log("setTimeout 0")},0);setTimeout(()=>{  console.log("setTimeout 100")},100);process.nextTick(()=>{  console.log("nextTick")  process.nextTick(()=>{    console.log("nextTick inner")  })});复制代码

看下执行结果:

$ node timer.js nextTicknextTick innersetTimeout 0setImmediatesetTimeout 100复制代码

process.nextTick()中的回调函数最快执行,因为它将异步事件插入到当前执行队列的末尾,但如果process.nextTick()中的事件执行时间过长,后面的异步事件就被延迟。

setImmediate()执行最慢,因为它将事件插入到下一个事件队列的队首,不会影响当前事件队列的执行。当setTimeout(fn, 0)是在setImmediate()之前执行。

2.4 Buffer 二进制

Buffer 对象用于处理二进制数据流。JS 没有处理二进制的功能,而 Node 中的一部分代码是由 C++ 实现的,所有 Node 中的 Buffer 性能部分用 C++ 实现,非性能部分由 JS 封装。

Buffer 实例类似整数数组,元素为十六进制的两位数(0~255),并且挂载在 global 对象上不需要 require就能使用。

最新的 Buffer API 使用Buffer.alloc(length, value)创建固定长度为 length 的 Buffer 实例,value 默认填充 0,使用Buffer.from()将其它类型数据转为 Buffer:

console.log(Buffer.alloc(5))        // 
console.log(Buffer.alloc(5, 44)) //
console.log(Buffer.from([3, 4, 5])) //
console.log(Buffer.from('test')) //
console.log(Buffer.from('测试')) //
复制代码

注意到字符串转 Buffer 时英文占一位,中文占三位,而不是四位,当中文乱码的时可以考虑没有正确读取 Buffer 流。

Buffer 类提供几个静态方法,Buffer.byteLength()计算长度,Buffer.isBuffer()做验证,Buffer.concat()拼接 Buffer 实例:

const buf1 = Buffer.from([3, 4, 5])const buf2 = Buffer.from('test')console.log(Buffer.byteLength('test'))   // 4console.log(Buffer.byteLength('测试'))    // 6 console.log(Buffer.isBuffer('test'))     // falseconsole.log(Buffer.isBuffer(buf1))       // trueconsole.log(Buffer.concat([buf1, buf2])) // 
复制代码

除此之外,Buffer 实例也有常用的属性和方法,类似 JS 中的 String,有lengthtoString('base64')equals()indexOf()等。

以上就是 Node 全局变量的概述,其他的 API 或内置模块都需要· require('xxx')引入使用,我们可以在 中查看关于 更详细的介绍。

三、基础 API

3.1 path 路径相关

path 是处理和路径相关问题的内置 API,可以直接require('path')使用。以下示例常用的 path 方法。

对路径的处理常用path.normalize()规范路径、path.join()拼接路径,以及使用path.resolve()将相对路径解析为绝对路径:

const path = require('path')console.log(  path.normalize('//asd\/das'), // /asd/das  path.join('user', 'local'),   // user/local  path.resolve('./'))           // /Users/Documents/webProjects/testNode/apiTest复制代码

解析某个路径,可以用path.basename()得到文件名称,path.extname()得到后缀扩展名,path.dirname()得到目录名:

const path = require('path')const filePath = 'webProjects/testNode/apiTest/path.js'console.log(  path.basename(filePath), // path.js   path.extname(filePath)   // .js   path.dirname(filePath),  // webProjects/testNode/apiTest)复制代码

以上解析路径方法得到某个值,还可以使用path.parse()完全解析路径为一个对象,path.format()反向操作:

let sp = path.parse(filePath)console.log(sp)// { root: '',//   dir: 'webProjects/testNode/apiTest',//   base: 'path.js',//   ext: '.js',//   name: 'path' }console.log(path.format(sp))// webProjects/testNode/apiTest/path.js复制代码

除此之外,还有对于系统路径的操作,使用path.sep取得路径分隔符,路径片段分隔符,POSIX 上是/, Windows 上是\path.delimiter取得系统路径定界符,POSIX 上是:,Windows 上是; ,示例如下:

console.log(filePath.split(path.sep)) // [ 'webProjects', 'testNode', 'apiTest', 'path.js' ]console.log(process.env.PATH) // 系统路径配置// /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinconsole.log(process.env.PATH.split(path.delimiter))// [ '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin' ]复制代码

以上就是 Node 对路径的常用操作,需要注意的是,在获取路径时有几种方式,得到的路径是不同的:

  • __dirname__filename:总是返回文件绝对路径;
  • process.cwd()$ pwd :返回执行 Node 命令的文件夹;
  • path.resolve('./'):是相对 Node 启动文件夹,在require()./是相对于当前文件夹;

3.3 events 事件

大部分 Node API 都采用异步事件驱动,所有能触发事件对象都是 EventEmitter 类的实例,通过 EventEmitter.on()绑定事件,然后通过 EventEmitter.emit() 触发事件。

// apiTest/events.jsconst Events = require('events')class MyEvents extends Events{}const event = new MyEvents()event.on('test-event',()=>{  console.log('this is an event')})event.emit('test-event')setInterval(()=>{  event.emit('test-event')},500)复制代码

执行以上代码会一直连续处罚 test-event 事件,当然还可以传递事件参数,并且可以传递多个参数。修改上诉代码如下:

event.on('test-event', (data, time) => {  console.log(data,time)})event.emit('test-event', [1, 2, 3], new Date())复制代码
$ node apiTest/events.js[ 1, 2, 3 ] 2019-04-23T07:28:00.420Z复制代码

同一个事件监听器可以绑定多个事件,触发时按照绑定顺序加入执行队列,并且可以使用EventEmitter.removeListener()删除监听器的事件:

function fn1 () {  console.log('fn1')}function fn2 () {  console.log('fn2')}event.on('multi-event',fn1)event.on('multi-event',fn2)setInterval(()=>{  event.emit('multi-event')},500)setTimeout(()=>{  event.removeListener('multi-event', fn2)}, 600)复制代码
$ node apiTest/events.js[ 1, 2, 3 ] 2019-04-23T07:39:11.624Zfn1fn2fn1fn1...复制代码

3.4 fs 文件系统

Node 文件模块通过require('fs)使用,所用方法都有同步和异步方法。

文件系统中的异步方法,第一个参数保留给异常,操作成功时参数值为nullundefined,最后一个参数就是回调函数。例如读取文件的fs.readFile()和写文件的fs.writeFile()示例如下:

const fs = require('fs')fs.readFile('./apiTest/fs.js', (err, data) => { if (err) throw err console.log('readFile done!!!')})fs.writeFile('./apiTest/fs.txt', 'this is test file', { encoding: 'utf8'}, (err) => { if (err) throw err console.log('writeFile done!!!')})复制代码

推荐 中的 查看更多 Node API 的使用。


加油呢少年~

转载地址:http://mihpl.baihongyu.com/

你可能感兴趣的文章
靠打赏盈利的简书网是自媒体的新型发展模式吗?
查看>>
查询改写参数配置
查看>>
Kubernetes 网络改进的三项实践分享
查看>>
SpringMVC的粗略整理(一)
查看>>
Visual Paradigm 教程[企业架构]:如何绘制ArchiMate图?
查看>>
Git 提交的正确姿势:Commit message 编写指南
查看>>
分享HTML5自动化构建工具gulp使用方法步骤
查看>>
巴塞尔委员会为有意加入加密市场的银行制定了指引
查看>>
PHP 包含文件
查看>>
Java发展历史
查看>>
BootStrap 资源汇总
查看>>
为Empathy增加QQ支持
查看>>
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit:
查看>>
rabbitmq的使用笔记
查看>>
QT临时笔记
查看>>
一次、二次、三次指数平滑计算思想及代码
查看>>
TIDB 最佳实践
查看>>
linux 中mysql命令使用
查看>>
Freemaker+html+css+common-email
查看>>
开发者账号使用个人总结
查看>>