NodeJs笔记
$ npm install express
var express = require('express');
npm install express # 本地安装
npm install express -g # 全局安装
如果出现以下错误:npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
$ npm config set proxy null
你可以使用以下命令来查看所有全局安装的模块:
$ npm list -g
如果要查看某个模块的版本号,可以使用命令如下:
$ npm list grunt
我们可以使用以下命令来卸载 Node.js 模块。
$ npm uninstall express
卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:
$ npm ls
我们可以使用以下命令更新模块:
$ npm update express
使用以下来搜索模块:
$ npm search express
创建模块
$ npm init
Press ^C at any time to quit. name: (admin) runoob version: (1.0.0) description: Node.js 测试模块(www.runoob.com) entry point: (index.js) test command: make test git repository: https://github.com/runoob/runoob.git keywords: 123 author: lcs license: (ISC) About to write to C:\Users\admin\package.json: { "name": "runoob", "version": "1.0.0", "description": "Node.js测试模块\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D 测试模块\u001b[D\u001b[D\u001b\u001b[C\u001b[C\u001b[C模块()\u001b[www.runoob.com)", "main": "index.js", "dependencies": {}, "devDependencies": {}, "scripts": { "test": "make test" }, "repository": { "type": "git", "url": "git+https://github.com/runoob/runoob.git" }, "keywords": [ "123" ], "author": "lcs", "license": "ISC", "bugs": { "url": "https://github.com/runoob/runoob/issues" }, "homepage": "https://github.com/runoob/runoob#readme" } Is this ok? (yes) yes
我们可以输入以下命令来启动 Node 的终端:
$ node
>
在 Node.js REPL 的命令行窗口中执行简单的数学运算:
$ node > 1 +4 5 > 5 / 2 2.5 > 3 * 6 18 > 4 - 1 3 > 1 + ( 2 * 3 )- 4 3 >
你可以将数据存储在变量中,并在你需要的时候使用它。
$ node > x = 1010 > var y = 10 undefined > x + y 20 > console.log("Hello World")Hello Worldundefined > console.log("www.runoob.com") www.runoob.com undefined
$ node > var x = 0undefined> do {... x++;... console.log("x: " + x);... } while ( x < 5 ); x: 1 x: 2 x: 3 x: 4 x: 5undefined>
$ node > var x = 10undefined > var y = 20undefined > x + y 30 > var sum = _ undefined > console.log(sum) 30 undefined >
REPL 命令
ctrl + c - 退出当前终端。 ctrl + c 按下两次 - 退出 Node REPL。 ctrl + d - 退出 Node REPL. 向上/向下 键 - 查看输入的历史命令 tab 键 - 列出当前命令 .help - 列出使用命令 .break - 退出多行表达式 .clear - 退出多行表达式 .save filename - 保存当前的 Node REPL 会话到指定文件 .load filename - 载入当前 Node REPL 会话的文件内容。
Node.js 异步编程的直接体现就是回调。
异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。
回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。
例如,我们可以一边读取文件,一边执行其他命令,在文件读取完成后,我们将文件内容作为回调函数的参数返回。这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。
非阻塞代码实例
setTimeout(cb, ms) 全局函数在指定的毫秒(ms)数后执行指定函数(cb)。:setTimeout() 只执行一次指定函数。
clearTimeout( t ) 全局函数用于停止一个之前通过 setTimeout() 创建的定时器。 参数 t 是通过 setTimeout() 函数创建的定时器。
setInterval(cb, ms) 全局函数在指定的毫秒(ms)数后执行指定函数(cb)。
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。
* console.error():与console.log() 用法相同,只是向标准错误流输出。
* console.trace():向标准错误流输出当前的调用栈。
console.trace(); 运行结果为: Trace: at Object.<anonymous> (/home/byvoid/consoletrace.js:1:71) at Module._compile (module.js:441:26) at Object..js (module.js:459:10) at Module.load (module.js:348:31) at Function._load (module.js:308:12) at Array.0 (module.js:479:10) at EventEmitter._tickCallback (node.js:192:40)
process 是一个全局变量,即 global 对象的属性。
它用于描述当前Node.js 进程状态的对象,提供了一个与操作系统的简单接口。通常在你写本地命令行程序的时候,少不了要 和它打交道。
1 stdout 标准输出流。 2 stderr 标准错误流。 3 stdin 标准输入流。 4 argv argv 属性返回一个数组,由命令行执行脚本时的各个参数组成。它的第一个成员总是node,第二个成员是脚本文件名,其余成员是脚本文件的参数。 5 execPath 返回执行当前脚本的 Node 二进制文件的绝对路径。 6 execArgv 返回一个数组,成员是命令行下执行脚本时,在Node可执行文件与脚本文件之间的命令行参数。 7 env 返回一个对象,成员为当前 shell 的环境变量 8 exitCode 进程退出时的代码,如果进程优通过 process.exit() 退出,不需要指定退出码。 9 version Node 的版本,比如v0.10.18。 10 versions 一个属性,包含了 node 的版本和依赖. 11 config 一个包含用来编译当前 node 执行文件的 javascript 配置选项的对象。它与运行 ./configure 脚本生成的 "config.gypi" 文件相同。 12 pid 当前进程的进程号。 13 title 进程名,默认值为"node",可以自定义该值。 14 arch 当前 CPU 的架构:'arm'、'ia32' 或者 'x64'。 15 platform 运行程序所在的平台系统 'darwin', 'freebsd', 'linux', 'sunos' 或 'win32' 16 mainModule require.main 的备选方法。不同点,如果主模块在运行时改变,require.main可能会继续返回老的模块。可以认为,这两者引用了同一个模块。