NodeJs 学习笔记
Node.js 是一个开源的、跨平台的 JavaScript 运行时环境。
官方提供的全局变量:
- __dirname 当前 JS 文件所在的目录的绝对路径
- __filename 当前 JS 文件所在的绝对路径
fs 模块
The node:fs
module enables interacting with the file system in a way modeled on standand POSIX functionsnode:fs
支持以标准POSIX函数为模型化的方式与文件系统交互。
1 | const fs = require('fs') |
[Error: ENOENT: no such file or directory, open ‘context.txt’] path
为相对路径时,./
指的是 node 运行环境的地址,如在 ‘/‘ 下运行 node ./src/index.js
当前目录是指根目录,/context.txt
不存在,所以找不到文件。
具体的 Demo 案例
fs.realpathSync(path[,options])
return the resolved pathname 返回已解析的路径名
- path
<string>
|<Buffer>
|<URL>
- options
<string>
|<Object>
- encoding
<string>
Default: ‘utf8’ - encoding utf8/buffer/base64
- encoding
- Returns:
<string>
|<Buffer>
fs - util
The node:util module supports the needs of Node.js internal APIs. Many of the utilities are useful for application and developers as well.
node:util 模块支持 Node.js 内部 APIs 的需求。许多实用程序对应用程序和模块开发人员是有用的
fs - util.promisify
1 | /** |
Takes a function following the common error-first callback style, i.e. taking an (err, value) => … callback as the last argument,and returns a version that returns promises.
采用遵循常见的错误优先样式的回调函数,也就是说采用 (err, value) => ...
回调函数作为参数,且返回 promises 版本。
1 | // 引入 util 模块 |
vm 模块
The node:vm module enables compiling and running code within V8 Virtual Machine context
vm 模块可以在 V8 虚拟机中编译运行代码
1 | vm.runInNewContext(code) |
1 | vm.runInNewContext(code, sandbox, "sea-debug.vm") |
module
http 模块
1 | // 1、导入 http 模块 |
path 模块
提供用于处理文件路径和目录路径的实用工具
1 | const path = require('path') |
- path.resolve()
将路径或片断序列解析为绝对路径
1
2
3
4
5
6// 不传参
path.resolve() // '/Users/lipingzhang/gitProject/blog'
path.resolve(1) // TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number
path.resolve('a','b') // '/Users/lipingzhang/gitProject/blog/a/b'
path.resolve('/temp','new') // '/temp/new'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif') // '/Users/lipingzhang/gitProject/blog/wwwroot/static_files/gif/image.gif' - path.join(
) 使用平台特定分隔符将所有给定 path 片段连接在一起,规范化生成路径
1
2path.join() // '.' 表示当前目录
path.join(process.cwd(), 'hexo.json') // '/Users/lipingzhang/Desktop/hexo-cli的副本/a.json'
process
The process object provides information about, and control over(对…的控制),the current Node.js process.
进程对象提供了当前 NodeJs 进程的相关信息和支配。
全局变量,提供有关当前 Node.js 进程信息,并对其进行控制,不需要 require
platform
chdir
process.cwd()
The process.cwd()
返回
process.env
argv
nextTick
Process events
Event: ‘unhandledRejection’
The ‘unhandledRejection’ event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop.
当 Promise 请求被拒绝且一轮事件循环中没有错误处理程序添加到 promise 时,发送 ‘unhandledRejection’
when programing with Promises, exceptions are encapsulated as ‘rejected promise’
当使用 Promise 编程时,异常被封装为 ‘rejected promise’
Rejections can be caught and handled usingpromise.catch()
and are propagated through aPromise
chain.
使用prmise.catch()
可以捕获并处理 Rejections,通过 promise chain 可以传播 rejections.
TheunhandledRejection
event is useful for detecting and keeping track of promises that were rejected whose rejections have not yet been handled.unhandledRejection
事件可以用于检测和跟踪那些未处理或拒绝的 rejections
1 | const process = require('node:process') |
process - 信号事件
Interrupt from keyboard
SIGINT 在终端运行时,可以被所有平台支持,通常可以通过
1 | process.on('SIGINT', function() { |