习题

今天好homie甩我个NodeJS的练习题,来看看怎么个事
题目

分析一下,该怎么实现呢?

我灵光乍现,NodeJS + Express刚刚好能够实现
Express快速搭建一个接口,NodeJS内置的fs可以处理同步和异步的读写、path可以读取路径
然后自定义 Express 中间件,最后用 JSON 文件存储书籍数据
怎么整呢?俺寻思也妹发个压缩包啊……
只能新建文件了
先写一个PY脚本,来生成10个书籍文件吧
里面的内容统一为This is Book NO.{i}
文件名就叫book_{i}.txt
____根目录
|————书籍
|——book{i}.txt
|————main.js
|____book.json

在 书籍 的根目录中新建一个py脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import os

def create_book():
base_dir = os.path.dirname(os.path.abspath(__file__))

for i in range(1, 11):
file_name = f"book_{i:03d}.txt"
file_path = os.path.join(base_dir, file_name)

try:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(f"This is book No.{i}\n")

print(f"创建文件: {file_path}")

except Exception as e:
print(f"创建文件 {file_path} 失败: {str(e)}")

if __name__ == "__main__":
create_book()

运行一下,这样就在 书籍 目录下创建了以下文件

1
2
3
4
5
6
7
8
9
10
book_001.txt
book_002.txt
book_003.txt
book_004.txt
book_005.txt
book_006.txt
book_007.txt
book_008.txt
book_009.txt
book_010.txt

接下来写初始的book.json,包含上述的文件属性

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
import json
import os

def generate_book_json():

book_list = []
book_dir = "./书籍"

if not os.path.exists(book_dir):
os.makedirs(book_dir)
print(f"创建目录: {book_dir}")

for i in range(1, 11):
num = f"{i:03d}"
book_info = {
"book": f"book{num}",
"author": num,
"summary": f"简介{num}",
"subDate": "2025-11-29"
}

book_file_path = os.path.join(book_dir, f"book{num}.txt")
if os.path.exists(book_file_path):
try:
with open(book_file_path, 'r', encoding='utf-8') as f:
file_content = f.read().strip()
book_info["file_content"] = file_content
book_info["summary"] = f"简介{num} - {file_content[:20]}..."
except Exception as e:
print(f"读取文件 {book_file_path} 失败: {e}")

book_list.append(book_info)


output_path = "./book.json"
try:
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(book_list, f, ensure_ascii=False, indent=4)
print(f"生成JSON文件: {output_path}")
print(f"生成 {len(book_list)} 条书籍信息")
except Exception as e:
print(f"写入JSON文件失败: {e}")

if __name__ == "__main__":
generate_book_json()

在book.json写入了以下文件

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
58
59
60
61
62
[
{
"book": "book001",
"author": "001",
"summary": "简介001",
"subDate": "2025-11-29"
},
{
"book": "book002",
"author": "002",
"summary": "简介002",
"subDate": "2025-11-29"
},
{
"book": "book003",
"author": "003",
"summary": "简介003",
"subDate": "2025-11-29"
},
{
"book": "book004",
"author": "004",
"summary": "简介004",
"subDate": "2025-11-29"
},
{
"book": "book005",
"author": "005",
"summary": "简介005",
"subDate": "2025-11-29"
},
{
"book": "book006",
"author": "006",
"summary": "简介006",
"subDate": "2025-11-29"
},
{
"book": "book007",
"author": "007",
"summary": "简介007",
"subDate": "2025-11-29"
},
{
"book": "book008",
"author": "008",
"summary": "简介008",
"subDate": "2025-11-29"
},
{
"book": "book009",
"author": "009",
"summary": "简介009",
"subDate": "2025-11-29"
},
{
"book": "book010",
"author": "010",
"summary": "简介010",
"subDate": "2025-11-29"
}
]

到这里算是把木有的东西补齐了

接下来开始捯饬一下

之前提到过可以使用express的方法实现