Marble.js 项目教程
1. 项目的目录结构及介绍
Marble.js 项目的目录结构如下:
marble/
├── assets/
├── benchmarks/
├── packages/
├── scripts/
├── .commitlintrc.js
├── .editorconfig
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .istanbul.yml
├── LICENSE
├── README.md
├── SECURITY.md
├── codecov.yml
├── docker-compose.yml
├── jest.config.js
├── lerna.json
├── package.json
├── tsconfig.json
├── tsconfig.test.json
└── yarn.lock
目录结构介绍
- assets/: 存放项目相关的静态资源文件。
- benchmarks/: 存放性能测试相关的文件。
- packages/: 存放项目的各个模块,如核心模块、HTTP模块、WebSocket模块等。
- scripts/: 存放项目的脚本文件,用于自动化任务。
- .commitlintrc.js: 配置文件,用于规范Git提交信息。
- .editorconfig: 配置文件,用于统一代码编辑器的设置。
- .eslintignore: 配置文件,用于指定ESLint忽略的文件或目录。
- .eslintrc.js: 配置文件,用于配置ESLint规则。
- .gitignore: 配置文件,用于指定Git忽略的文件或目录。
- .istanbul.yml: 配置文件,用于配置代码覆盖率工具Istanbul。
- LICENSE: 项目的开源许可证文件。
- README.md: 项目的介绍文档。
- SECURITY.md: 项目的安全相关文档。
- codecov.yml: 配置文件,用于配置代码覆盖率服务Codecov。
- docker-compose.yml: 配置文件,用于配置Docker容器。
- jest.config.js: 配置文件,用于配置Jest测试框架。
- lerna.json: 配置文件,用于配置Lerna多包管理工具。
- package.json: 项目的npm配置文件,包含项目的依赖、脚本等信息。
- tsconfig.json: 配置文件,用于配置TypeScript编译选项。
- tsconfig.test.json: 配置文件,用于配置TypeScript测试编译选项。
- yarn.lock: 锁定文件,用于锁定项目依赖的版本。
2. 项目的启动文件介绍
Marble.js 项目的启动文件通常位于 packages/
目录下的某个模块中。以 @marblejs/core
模块为例,启动文件通常是一个 TypeScript 文件,例如 main.ts
。
启动文件示例
import { createServer } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';
import { bodyParser$ } from '@marblejs/middleware-body';
import { httpListener } from '@marblejs/http';
import { api$ } from './api';
const middlewares = [
logger$(),
bodyParser$(),
];
const effects = [
api$,
];
const listener = httpListener({
middlewares,
effects,
});
const server = createServer({
port: 1337,
hostname: '127.0.0.1',
listener,
});
server.run();
启动文件介绍
- createServer: 创建HTTP服务器的函数,配置端口、主机名等参数。
- logger$: 日志中间件,用于记录HTTP请求和响应。
- bodyParser$: 请求体解析中间件,用于解析HTTP请求体。
- httpListener: 配置HTTP监听器,包含中间件和路由效果。
- api$: 定义API路由的效果函数。
3. 项目的配置文件介绍
Marble.js 项目的配置文件主要包括以下几个:
1. .commitlintrc.js
module.exports = {
extends: ['@commitlint/config-conventional'],
};
2. .editorconfig
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
3. .eslintrc.js
module.exports = {
extends: ['eslint:recommended'],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
rules: {
// 自定义规则
},
};
4. .gitignore
node_modules/
dist/
*.log
5. .istanbul.yml
instrumentation:
root: src
excludes: ['**/*.spec.ts']
6. package.json
{
"name": "marblejs",
"version": "1.0.0",
"scripts": {
"start": "node dist/main.js",
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@marblejs/core": "^4.0.0",
"@marblejs/http": "^4.0.0"
},
"devDependencies": {
"typescript": "^4.0.0",
"jest": "^26.0.0"
}
}
7. tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
8. tsconfig.test.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts"]
}
配置文件介绍
- .commitlintrc.js: 配置Git提交信息的规范。
- .editorconfig: 配置代码编辑器的统一设置。
- .eslintrc.js: 配置ESLint规则。
- .gitignore: 指定Git忽略的文件或目录。
- .istanbul.yml: 配置代码覆盖率工具Istanbul。
- package.json: 项目的npm配置文件,包含项目的依赖、脚本等信息。
- tsconfig.json: 配置TypeScript编译选项。
- tsconfig.test.json: 配置TypeScript测试编译选项。
通过以上配置文件,可以确保项目在开发、测试、部署等各个环节的一致性和规范性。