首页
/ 开源项目 `typed-fp-good-reads` 使用教程

开源项目 `typed-fp-good-reads` 使用教程

2024-08-30 06:12:31作者:齐冠琰

1. 项目的目录结构及介绍

typed-fp-good-reads/
├── README.md
├── package.json
├── src/
│   ├── index.ts
│   ├── config/
│   │   ├── default.json
│   │   ├── production.json
│   ├── modules/
│   │   ├── books/
│   │   │   ├── books.controller.ts
│   │   │   ├── books.service.ts
│   │   │   ├── books.model.ts
│   │   ├── authors/
│   │   │   ├── authors.controller.ts
│   │   │   ├── authors.service.ts
│   │   │   ├── authors.model.ts
├── test/
│   ├── books.test.ts
│   ├── authors.test.ts

目录结构说明

  • README.md: 项目说明文档。
  • package.json: 项目依赖和脚本配置文件。
  • src/: 源代码目录。
    • index.ts: 项目入口文件。
    • config/: 配置文件目录。
      • default.json: 默认配置文件。
      • production.json: 生产环境配置文件。
    • modules/: 功能模块目录。
      • books/: 书籍相关模块。
        • books.controller.ts: 书籍控制器。
        • books.service.ts: 书籍服务。
        • books.model.ts: 书籍模型。
      • authors/: 作者相关模块。
        • authors.controller.ts: 作者控制器。
        • authors.service.ts: 作者服务。
        • authors.model.ts: 作者模型。
  • test/: 测试文件目录。
    • books.test.ts: 书籍模块测试。
    • authors.test.ts: 作者模块测试。

2. 项目的启动文件介绍

项目的启动文件是 src/index.ts。该文件主要负责初始化应用、加载配置、启动服务器等操作。以下是 index.ts 的主要内容:

import express from 'express';
import { config } from 'dotenv';
import { connectDB } from './config/db';
import { booksRouter } from './modules/books/books.controller';
import { authorsRouter } from './modules/authors/authors.controller';

config();
connectDB();

const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());
app.use('/api/books', booksRouter);
app.use('/api/authors', authorsRouter);

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

启动文件说明

  • import express from 'express': 导入 Express 框架。
  • import { config } from 'dotenv': 导入 dotenv 配置模块。
  • import { connectDB } from './config/db': 导入数据库连接模块。
  • import { booksRouter } from './modules/books/books.controller': 导入书籍控制器路由。
  • import { authorsRouter } from './modules/authors/authors.controller': 导入作者控制器路由。
  • config(): 加载环境变量。
  • connectDB(): 连接数据库。
  • const app = express(): 创建 Express 应用实例。
  • const port = process.env.PORT || 3000: 设置端口号。
  • app.use(express.json()): 使用 JSON 中间件。
  • app.use('/api/books', booksRouter): 挂载书籍路由。
  • app.use('/api/authors', authorsRouter): 挂载作者路由。
  • app.listen(port, () => { console.log(Server is running on port ${port}) }): 启动服务器并监听端口。

3. 项目的配置文件介绍

项目的配置文件位于 src/config/ 目录下,主要包括 default.jsonproduction.json 两个文件。

default.json

{
  "
登录后查看全文
热门项目推荐