| 语言 | 格式 | 评分 |
|---|---|---|
中文(简体) | .pdf | 3 |
| 摘要 | ||
文档介绍了Node.js的基本使用,包括创建HTTP服务器、使用内置模块和第三方模块、处理二进制数据(Buffer对象)、网络请求(http.Client)以及扩展Node.js的方法。文档还提到了如何使用crypto模块进行签名和验证。 | ||
| AI总结 | ||
### Node.js 中文文档总结
#### 1. **简介**
Node.js 是基于 Chrome V8 引擎的 JavaScript 运行时环境,用于构建高效的服务器端应用。
**示例**:
```javascript
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
```
将代码保存为 `example.js`,运行命令 `node example.js` 即可启动服务。
---
#### 2. **标准模块**
Node 附带了一些内置模块,可通过 `require('module-name')` 使用。
- **示例**:`var sys = require('sys');`
- 常用模块包括 `http`、`fs`、`path` 等。
---
#### 3. **Buffer 缓存对象**
Buffer 用于处理二进制数据流,类似于数组,但直接操作内存。
- 创建方式:`var buf = new Buffer(256);`
- 支持编码方式:`utf8`、`ascii`、`base64` 等。
- **注意**:`binary` 编码已废弃。
---
#### 4. **进程信息**
Node 提供 `process` 模块,用于获取和操作进程信息:
- `process.cwd()`:返回当前工作目录。
- `process.env`:获取环境变量。
- `process.exit(code)`:退出进程,`code` 默认为 0。
- `process.getgid()` 和 `process.setgid()`:获取/设置用户组标识。
---
#### 5. **HTTP 服务器**
Node 的 `http` 模块用于创建 HTTP 服务器:
- **响应头**:`response.writeHead(statusCode, headers)`,必须在 `response.end()` 之前调用。
- **响应体**:`response.write(chunk)` 和 `response.end([data])` 用于发送数据。
- **示例**:
```javascript
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(8124);
```
---
#### 6. **HTTP 客户端**
Node 提供 `http.createClient` 创建 HTTP 客户端:
- **示例**:
```javascript
var http = require('http');
var client = http.createClient(80, 'www.google.com');
var request = client.request('GET', '/');
request.end();
request.on('response', function (response) {
console.log('STATUS:', response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY:', chunk);
});
});
```
---
#### 7. **模块系统**
Node 使用 CommonJS 模块系统,文件即为模块:
- **导出**:`exports.property = value;` 或 `module.exports = value;`
- **导入**:`var module = require('module-name');`
- **示例**:
```javascript
// circle.js
exports.area = function (r) {
return Math.PI * r * r;
};
```
---
#### 8. **第三方模块**
Node 的第三方模块可通过 npm 安装和管理:
- **常用模块**:
- HTTP 中间件:Connect
- Web 框架:Express
- WebSocket:Socket.IO
- 数据库:mysql、pg
- 测试:vows、mocha
- **安装**:`npm install module-name`
---
#### 9. **加密功能**
Node 提供 `crypto` 模块用于加密操作:
- **签名**:`crypto.createSign(algorithm).update(data).sign(privateKey)`
- **验证**:`crypto.createVerify(algorithm).update(data).verify(publicKey, signature)`
- 支持的算法:`RSA-SHA256`、`ECDSA-SHA256` 等。
---
#### 10. **DNS 解析**
Node 提供 `dns` 模块用于域名解析:
- **正向解析**:`dns.resolve(hostname, callback)`
- **反向解析**:`dns.reverse(ip, callback)`
- **示例**:
```javascript
var dns = require('dns');
dns.resolve('www.google.com', function (err, addresses) {
addresses.forEach(function (a) {
dns.reverse(a, function (err, domains) {
console.log('Reverse for', a, ':', domains);
});
});
});
```
---
#### 11. **附录**
文档末尾提供了第三方模块的 Wiki 页面和推荐资源,帮助用户快速查找和使用高质量模块。 | ||
P1
P2
P3
P4
P5
P6
P7
下载文档到本地,方便使用
- 可预览页数已用完,剩余
55 页请下载阅读 -
文档评分














NodeJS 中文文档 V0.2.3
httpd 2.4.10 中文文档