Duniter 项目使用教程
1. 项目的目录结构及介绍
Duniter 项目的目录结构如下:
duniter/
├── docs/
├── src/
│ ├── api/
│ ├── blockchain/
│ ├── core/
│ ├── database/
│ ├── network/
│ ├── node/
│ ├── server/
│ ├── utils/
│ └── main.ts
├── config/
│ ├── default.json
│ ├── production.json
│ └── test.json
├── package.json
├── tsconfig.json
└── README.md
目录介绍
- docs/: 包含项目的文档文件。
- src/: 包含项目的源代码。
- api/: 包含 API 相关的代码。
- blockchain/: 包含区块链相关的代码。
- core/: 包含核心功能的代码。
- database/: 包含数据库相关的代码。
- network/: 包含网络通信相关的代码。
- node/: 包含节点相关的代码。
- server/: 包含服务器相关的代码。
- utils/: 包含工具函数和辅助代码。
- main.ts: 项目的启动文件。
- config/: 包含项目的配置文件。
- default.json: 默认配置文件。
- production.json: 生产环境配置文件。
- test.json: 测试环境配置文件。
- package.json: 项目的依赖管理文件。
- tsconfig.json: TypeScript 配置文件。
- README.md: 项目说明文件。
2. 项目的启动文件介绍
项目的启动文件是 src/main.ts
。该文件负责初始化项目并启动服务器。以下是 main.ts
的主要功能:
import { startServer } from './server';
async function main() {
await startServer();
}
main().catch(console.error);
主要功能
- 导入
startServer
函数: 从server
模块导入startServer
函数。 - 定义
main
函数: 异步函数main
调用startServer
函数启动服务器。 - 捕获错误: 使用
catch
捕获并打印错误。
3. 项目的配置文件介绍
项目的配置文件位于 config/
目录下,包含以下文件:
- default.json: 默认配置文件,包含所有环境的通用配置。
- production.json: 生产环境配置文件,覆盖默认配置中的特定设置。
- test.json: 测试环境配置文件,覆盖默认配置中的特定设置。
配置文件示例
default.json:
{
"server": {
"port": 8080
},
"database": {
"host": "localhost",
"port": 27017,
"name": "duniter"
}
}
production.json:
{
"server": {
"port": 80
},
"database": {
"host": "prod-db-host",
"port": 27017,
"name": "duniter-prod"
}
}
test.json:
{
"server": {
"port": 3000
},
"database": {
"host": "test-db-host",
"port": 27017,
"name": "duniter-test"
}
}
配置文件加载顺序
- 默认情况下,项目会加载
default.json
中的配置。 - 根据运行环境(如
NODE_ENV=production
),项目会加载相应的环境配置文件(如production.json
),并覆盖default.json
中的相同配置项。
通过以上配置文件,可以灵活地管理不同环境下的项目配置。