ProvableHQ SDK 使用教程
1. 项目的目录结构及介绍
sdk/
├── README.md
├── package.json
├── src/
│ ├── index.js
│ ├── config/
│ │ ├── default.json
│ │ ├── production.json
│ ├── utils/
│ │ ├── helper.js
│ ├── services/
│ │ ├── api.js
├── tests/
│ ├── index.test.js
- README.md: 项目说明文档。
- package.json: 项目依赖和脚本配置文件。
- src/: 源代码目录。
- index.js: 项目入口文件。
- config/: 配置文件目录。
- default.json: 默认配置文件。
- production.json: 生产环境配置文件。
- utils/: 工具函数目录。
- helper.js: 辅助函数文件。
- services/: 服务层代码目录。
- api.js: API 服务文件。
- tests/: 测试代码目录。
- index.test.js: 入口文件的测试代码。
2. 项目的启动文件介绍
项目的启动文件是 src/index.js
。这个文件负责初始化项目并启动服务。以下是 index.js
的主要内容:
const express = require('express');
const config = require('config');
const apiService = require('./services/api');
const app = express();
const port = config.get('port');
app.use('/api', apiService);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
- 引入依赖: 引入了
express
、config
和apiService
。 - 创建 Express 应用: 使用
express()
创建应用实例。 - 配置端口: 从配置文件中获取端口。
- 挂载 API 服务: 将
apiService
挂载到/api
路径。 - 启动服务: 监听指定端口并输出启动日志。
3. 项目的配置文件介绍
项目的配置文件位于 src/config/
目录下,主要包括 default.json
和 production.json
。
default.json
{
"port": 3000,
"database": {
"host": "localhost",
"port": 5432,
"name": "mydb"
}
}
- port: 默认端口号。
- database: 数据库连接配置。
production.json
{
"port": 8080,
"database": {
"host": "prod-db-host",
"port": 5432,
"name": "prod-db"
}
}
- port: 生产环境端口号。
- database: 生产环境数据库连接配置。
配置文件使用 config
模块进行管理,可以根据环境变量加载不同的配置文件。