Wing 项目使用教程
wing项目地址:https://gitcode.com/gh_mirrors/wing/wing
1. 项目的目录结构及介绍
Wing 项目的目录结构如下:
wing/
├── README.md
├── package.json
├── src/
│ ├── index.js
│ ├── config/
│ │ ├── default.json
│ │ ├── production.json
│ ├── routes/
│ │ ├── api.js
│ ├── models/
│ │ ├── user.js
│ ├── controllers/
│ │ ├── userController.js
│ ├── utils/
│ │ ├── helper.js
目录结构介绍
README.md
: 项目说明文件。package.json
: 项目依赖和脚本配置文件。src/
: 源代码目录。index.js
: 项目启动文件。config/
: 配置文件目录。default.json
: 默认配置文件。production.json
: 生产环境配置文件。
routes/
: 路由文件目录。api.js
: API 路由文件。
models/
: 数据模型文件目录。user.js
: 用户模型文件。
controllers/
: 控制器文件目录。userController.js
: 用户控制器文件。
utils/
: 工具函数文件目录。helper.js
: 辅助函数文件。
2. 项目的启动文件介绍
项目的启动文件是 src/index.js
。该文件主要负责以下内容:
- 引入必要的模块和配置文件。
- 初始化数据库连接。
- 配置路由。
- 启动服务器。
以下是 src/index.js
的示例代码:
const express = require('express');
const mongoose = require('mongoose');
const config = require('config');
const apiRoutes = require('./routes/api');
const app = express();
const port = process.env.PORT || 3000;
// 连接数据库
mongoose.connect(config.get('mongoURI'), { useNewUrlParser: true, useUnifiedTopology: true });
// 配置中间件
app.use(express.json());
// 配置路由
app.use('/api', apiRoutes);
// 启动服务器
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
3. 项目的配置文件介绍
项目的配置文件位于 src/config/
目录下,主要包括 default.json
和 production.json
。
default.json
默认配置文件,包含开发环境的配置信息。
{
"port": 3000,
"mongoURI": "mongodb://localhost:27017/wing"
}
production.json
生产环境配置文件,包含生产环境的配置信息。
{
"port": 8080,
"mongoURI": "mongodb://production-db:27017/wing"
}
配置文件主要包含以下内容:
port
: 服务器监听的端口号。mongoURI
: MongoDB 数据库连接字符串。
通过这些配置文件,可以方便地在不同环境下切换配置,确保项目的灵活性和可维护性。