一、环境
commonJS
nodejs模块管理规范- 每一个文件是一个模块,有自己的作用域
- 在模块内部
module变量代表模块本身 module.exports属性代表模块对外接口
exports与module.exports
exports = module.exportsexports是module.exports的快捷方式,不能修改exports的指向
// 这种方式修改了exports的指向,这样是不行的 |
// 这样没问题 |
global
全局访问的属性和方法挂载到global对象中
global下的全局属性方法commonJSBuffer、process、consoletimer(setInterval、setTimeout、setImmediate)
process
- process.argv
- 参数相关的东西
argv argv0是argv的第一个参数execArgv是调用node所传入的一些参数 如node --inspect test.jsexevPath调用脚本的路径C:\Program Files\nodejs\node.exe>node test3.js a=1 a=2 a=3 a=4执行的时候,传入一些外部的命令
- 参数相关的东西
const {argv, argv0, execArgv, exevPath} = process; |
- process.env
const {env} = process; |
- process.cwd
- 打印当前路径
C:\Users\Administrator\Desktop\learn_node |
- process.nextTick与setImmediate
- 执行速度:
nextTick>setTimeout>setImmediate nextTick在异步之前执行
- 执行速度:
// nextTick比setImmediate执行得早 |
二、调试
方法一:使用inspect
- Option 1: Open
chrome://inspectin a Chromium-based browser. Click the Configure button and ensure your target host and port are listed. Then select your Node.js app from the list - Option 2:
Install the ChromeExtension NIM (Node Inspector Manager): https://chrome.google.com/webstore/detail/nim-node-inspector-manage/gnhhdgbaldcilmgcpfddgdbkhjohddkj
方法二:使用VS code