Node.js 事实上就是一种上下文,它允许在后端(脱离浏览器环境)运行 JavaScript 代码。 要实现在后台运行 JavaScript 代码,代码需要先被解释然后正确的执行。 Node.js 使用 Google 的 V8 虚拟机来解释和执行 JavaScript 代码。 除此之外,Node.js 的还有许多有用的模块,可以简化很多重复的劳作,比如向终端输出字符串。 因此,Node.js 事实上既是一个运行时环境,同时又是一个库。
版本号格式 X.Y.Z,本别代表主版本号、次版本号和补丁版本号。
当代码有修改时,需按以下规则执行版本号变更:
^0.1.0 = [0.1.0, 1.0.0) ~0.1.0 = [0.1.0, 0.2.0) 0.1.0 = 0.1.0 >=0.1.0 = [0.1.0, 无穷大) * = 任意版本
回调函数的第一个参数永远是Error对象 func(param..., callback(Error, data)) Promise(function(resolve, reject): Promise{ //异步逻辑 }) Promise的状态 pending; fulfilled; reject Promise.prototype.then = function(onFulfilled[, onRejected]): Promise onFulfilled Promise 执行成功时回调 onRejected Promise 执行出错时回调,该参数是可选的 then方法: 返回一个新的 Promise 对象,因此 Promise 支持链式调用 Promise.prototype.catch = function(onRejected): Promise
event 基本使用步骤 实例化事件监听器实例,注册事件,触发事件 fs 读readFile() 写writeFile() 增appendFile(path, content) 删unlink(path) stream 读取流 readStream = fs.createReadStream('path.name', {encoding:'utf-8'}) 写入流 writeStream = fs.createWriteStream() 管道流 readStream.pipe(writeStream) 数据转换流 http
grunt-cli
并不等于安装了 Grunt!
Grunt CLI的任务很简单:调用与Gruntfile
在同一目录中 Grunt。
这样带来的好处是,允许你在同一个系统上同时安装多个版本的 Grunt。
package.json
和Gruntfile
后:
npm install
命令安装项目依赖的库。grunt
命令。where npm
D:\greenSoft\nodejs\npm
D:\greenSoft\nodejs\npm.cmd
D:\greenSoft\nodejs\node_global\npm
D:\greenSoft\nodejs\node_global\npm.cmd
npm root -g
# Print the effective node_modules folder to standard out.
D:\greenSoft\nodejs\node_global\node_modules
node -v
v20.9.0
npm -v
10.2.3
npm config get cache D:\greenSoft\nodejs\node_cache C:\Users\admin>npm config get prefix D:\greenSoft\nodejs\node_global
npm list -g
D:\greenSoft\nodejs\node_global
+-- cnpm@9.2.0
+-- electron@27.0.3
+-- http-server@14.1.1
+-- npm@10.2.3
`-- yarn@1.22.19
设置方法: npm config set prefix "D:\greenSoft\nodejs\node_global" 也可在文件 node_modules\npm\.npmrc 中添加 prefix=D:\greenSoft\nodejs\node_global