0080. http 模块概述
- 1. 📒 概述
- 2. 💻 demos.1 -
server
对象 - 3. 💻 demos.2 -
response
对象 - 4. 💻 demos.3 - 响应 html 文件
- 5. 💻 demos.4 - 响应媒体资源
- 6. 💻 demos.5 - 重定向
- 7. 💻 demos.6 -
request
对象
1. 📒 概述
- http 模块中主要有
server
对象、response
对象和request
对象,也是本节笔记主要介绍的内容。
2. 💻 demos.1 - server
对象
server
对象用来创建一个服务。- 在 Node.js 中,使用
http
模块中的createServer()
方法,可以创建一个server
对象const server = require('http').createServer()
server
对象中主要使用的方法有listen()
方法和close()
方法,它们分别控制着服务器的启动和结束。server.listen(port)
启动服务器,并监听指定端口。- 端口
port
是计算机与计算机之间信息的通道。 - 计算机中的端口从
0
开始,一共有65535
个端口。
- 端口
server.close()
关闭服务器。
js
// 引入 http 模块并创建 server 对象
const http = require('http')
// 辅助函数:获取当前时间的格式化字符串(亚洲上海本地时间)
const getCurrentTime = () => {
const now = new Date()
return now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
}
const server = http.createServer((req, res) => {
// 设置响应头,指定字符编码为 UTF-8,以防乱码
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
// 返回中文消息
res.end('服务器正在运行中...\n')
})
// 启动服务器并监听指定端口
const PORT = 23523
server.listen(PORT, () => {
console.log(
`${getCurrentTime()} 服务器已启动,监听地址是 http://127.0.0.1:${PORT}`
)
console.log(`${getCurrentTime()} 服务器将在10秒后关闭...`)
// 定时关闭服务器
setTimeout(() => {
server.close(() => {
console.log(`${getCurrentTime()} 服务器已成功关闭`)
})
}, 10 * 1000)
})
// 输出:
// 2025/4/24 20:57:50 服务器已启动,监听地址是 http://127.0.0.1:23523
// 2025/4/24 20:57:50 服务器将在10秒后关闭...
// 2025/4/24 20:58:00 服务器已成功关闭
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- 在服务启动期间访问:
http://127.0.0.1:23523
- 上述程序的大致流程:
- 引入
http
模块并创建服务器对象。 - 服务器接收到请求时,设置响应头
Content-Type
为text/plain; charset=utf-8
Content-Type
设置为文本类型text/plain
设置为纯文本类型charset=utf-8
设置为 UTF-8 编码,以避免乱码。
res.end('服务器正在运行中...\n')
- 并返回中文消息 "服务器正在运行中..."。
- 这里的
res
其实就是response
对象。
- 服务器监听端口
23523
,启动后输出监听地址。we
的生日是2023.05.23
- 启动后 10 秒自动关闭服务器。
- 关闭服务器后输出 "服务器已成功关闭"。
- 引入
3. 💻 demos.2 - response
对象
response
对象用于向客户端发送响应。- 主要方法:
writeHead(statusCode [,statusMessage] [,headers])
:设置响应头。statusCode
:数字类型的 HTTP 状态码。statusMessage
:HTTP 状态码对应的消息。headers
:响应头对象。
end([data], [encoding])
:结束响应并发送数据到客户端。data
:可选参数,执行完毕后要发送的字符。encoding
:可选参数,数据编码格式。
js
// 创建 Web 服务器,并监听 23523 端口
require('http')
.createServer(function (request, response) {
// 返回响应内容
response.writeHead(200, { 'Content-Type': 'text/html' })
response.end('<h1>Hello,Node.js</h1>')
})
.listen(23523, function () {
console.log('服务器监听地址是 http://127.0.0.1:23523')
})
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
4. 💻 demos.3 - 响应 html 文件
js
const fs = require('fs')
const http = require('http')
const path = require('path')
// 创建服务器
http
.createServer(function (request, response) {
// 读取 HTML 文件内容
fs.readFile(path.resolve(__dirname, '1.html'), function (error, data) {
response.writeHead(200, { 'Content-Type': 'text/html' })
response.end(data)
})
})
.listen(23523, function () {
console.log('服务器监听地址是 http://127.0.0.1:23523')
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>特殊文字符号</title>
<style>
h1,
pre {
text-align: center;
}
</style>
</head>
<body>
<h1>汪汪!你想找的页面让我吃喽!</h1>
<!--绘制可爱小狗的字符画-->
<pre>
.----.
_.'__ `.
.--($)($$)---/#\
.' @ /###\
: , #####
`-..__.-' _.-\###/
`;_: `"'
.'"""""`.
/, hi ,\\
// 你好! \\
`-._______.-'
___`. | .'___
(______|______)
</pre
>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
5. 💻 demos.4 - 响应媒体资源
js
const fs = require('fs')
const http = require('http')
const path = require('path')
const PORT = 23523
http
.createServer(async (request, response) => {
let filePath
let contentType
if (request.url === '/image') {
filePath = path.join(__dirname, '1.png')
contentType = 'image/png'
} else if (request.url === '/video') {
filePath = path.join(__dirname, '1.mp4')
contentType = 'video/mp4'
} else {
response.writeHead(404, { 'Content-Type': 'text/plain' })
response.end('Not Found')
return
}
try {
// 获取文件元信息
const stat = await fs.promises.stat(filePath)
const fileSize = stat.size
// 设置响应头
response.writeHead(200, {
'Content-Length': fileSize,
'Content-Type': contentType,
})
// 创建文件流并将其管道传输到响应对象
const fileStream = fs.createReadStream(filePath)
fileStream.on('error', (err) => {
console.error('文件流读取错误:', err.message)
if (!response.headersSent) {
response.writeHead(500, { 'Content-Type': 'text/plain' })
response.end('Internal Server Error')
}
})
fileStream.pipe(response)
} catch (error) {
console.error('文件元信息获取错误:', error.message)
if (!response.headersSent) {
response.writeHead(500, { 'Content-Type': 'text/plain' })
response.end('Internal Server Error')
}
}
})
.listen(PORT, () => {
console.log(`服务器监听位置是 http://127.0.0.1:${PORT}`)
})
// 写该 demo 的日期:2025年4月25日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
- 最终效果:
- 备注:
- 其中
1.mp4
是0032. 《Node.js 从入门到精通》
中的视频11.3 http 模块.mp4
的开头部分。
- 其中
6. 💻 demos.5 - 重定向
js
const http = require('http')
const PORT = 23523
const REDIRECT_URL = 'https://tdahuyou.github.io/notes/'
// 创建服务器
const server = http.createServer((request, response) => {
// 设置响应头:302 重定向
response.writeHead(302, { Location: REDIRECT_URL })
response.end() // 结束响应
})
// 启动服务器并监听端口
server.listen(PORT, () => {
console.log(`服务器已启动,监听地址:http://127.0.0.1:${PORT}`)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 访问:http://127.0.0.1:23523
writeHead
的第一个参数是statusCode
状态码,其数据类型是number
。- 常见的状态码及其含义:
状态码 | 说明 | 举例 |
---|---|---|
1** | 处理中 | 100 Continue |
2** | 成功 | 200 OK |
3** | 重定向 | 302 Temporarily Moved |
4** | 客户端错误 | 400 Bad Request |
5** | 服务器端错误 | 500 Internal Server Error |
7. 💻 demos.6 - request
对象
request
对象用于处理客户端请求。- 主要属性:
method
:请求方法(如GET
,POST
等)。url
:请求的 URL。headers
:请求头对象。
- 主要方法:
on(event, listener)
:监听请求事件。event
:事件名称,比如data
、end
等。listener
:事件处理函数。
js
const http = require('http')
const fs = require('fs').promises
const path = require('path')
const PORT = 23523
const HTML_FILE_PATH = path.resolve(__dirname, '1.html') // 定义 HTML 文件路径
// 创建服务器
const server = http.createServer(async (request, response) => {
try {
if (request.method === 'GET') {
// 处理 GET 请求:返回 HTML 文件内容
const data = await fs.readFile(HTML_FILE_PATH, 'utf-8')
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
response.end(data)
} else if (request.method === 'POST') {
// 处理 POST 请求:接收数据并返回响应
let body = ''
request.on('data', (chunk) => {
body += chunk.toString() // 累积接收到的数据
})
request.on('end', () => {
// 数据接收完成后返回响应
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
response.end(`<h1>${body}</h1>`)
})
} else {
// 不支持的请求方法
response.writeHead(405, { 'Content-Type': 'text/plain; charset=utf-8' })
response.end('Method Not Allowed')
}
} catch (error) {
console.error('服务器错误:', error.message)
response.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' })
response.end('Internal Server Error')
}
})
// 启动服务器并监听端口
server.listen(PORT, () => {
console.log(`服务器已启动,监听地址:http://127.0.0.1:${PORT}`)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>用户登录</title>
</head>
<body>
<main>
<section role="form">
<header>
<h1>用户登录</h1>
</header>
<form method="post" aria-label="用户登录表单">
<div>
<input type="text" name="login" placeholder="用户名" required />
</div>
<div>
<input
type="password"
name="password"
placeholder="密码"
required
/>
</div>
<div>
<label>
<input type="checkbox" name="remember_me" id="remember_me" />
记住密码
</label>
</div>
<div>
<button type="submit">登录</button>
</div>
</form>
</section>
</main>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- 测试:
- 用户名:
111
- 密码:
222
- 记住密码:
是
- 点击【登录】后:
- 用户名: