首页 > 解决方案 > 带有socketIO的expressJs中的最佳文件夹结构

问题描述

什么是项目的最佳文件夹结构,包括:

标签: node.jsmongodbexpresssocket.io

解决方案


这一切都取决于个人喜好。使您的文件夹结构以您最舒适的方式工作。

例如,我喜欢根据代码的用途将我的代码拆分为子模块或简单地说位于不同的目录中。

我个人会使用几个文件夹:

.{src}
├── controllers                # All controller operations according the routes are stored here
│   ├── authController.ts      # Handles authentication requests
│   ├── usersController.ts     # Handles users route requests
│   └── ...
│
├── database                   # All database connections are stored here. For example you have two databases
│   ├── db.ts                  # Initialize DB connection
│   └── ...
│
├── middleware
│   ├── authenticated.ts       # Decode and verify JWT token
│   ├── error.ts               # Common Error Handler
│   ├── logger.ts              # Control logging levels
│   └── ...
│
├── models                     # Simple descriptor of the database tables
│   ├── usersModel.ts          # DB model for users
│   └── ...
│
├── schema                     # Schemas that are used for CRUD operations with the models
│   ├── users.ts               # DB Schema for users
│   └── ...
│
├── listeners
│   ├── socketsManager.ts      # Socket listeners/emitters handle
│   └── ...
│
├── app.ts                     # Entry file for the project
├── .env                       # Store environment variables
├── routes.ts                  # All routes initializer
└── ...

尽管您可能不喜欢我的命名或顺序,但您可以创建自己喜欢的名称。您可能要做的另一件事是查看互联网上的一些约定或一些最流行的项目,如框架。

毕竟,这完全取决于您的需求以及您使用的舒适度。


推荐阅读