完成内容: - 创建前后端项目目录结构 - 初始化 Node.js + TypeScript 后端项目 (Fastify) - 初始化 Vue 3 + TypeScript 前端项目 (Vite) - 配置 Prisma ORM 和 MySQL 数据库 - 创建数据库 Schema 和种子数据 - 实现后端基础模块 (配置、日志、错误处理、验证) - 创建基础服务类 (BaseService) - 配置 Git 仓库和忽略文件 技术栈: - 后端:Fastify + Prisma + MySQL + Winston - 前端:Vue 3 + TypeScript + Element Plus + Pinia Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
395 lines
6.7 KiB
Markdown
395 lines
6.7 KiB
Markdown
# 任务 01:项目初始化
|
||
|
||
## 任务概述
|
||
创建项目基础结构,初始化前后端项目,配置开发环境。
|
||
|
||
## 前置条件
|
||
- Node.js 20+ 已安装
|
||
- MySQL 5.6 已安装并运行
|
||
- PM2 已全局安装
|
||
|
||
## 任务目标
|
||
- 创建项目目录结构
|
||
- 初始化后端项目(Node.js + TypeScript + Fastify)
|
||
- 初始化前端项目(Vue 3 + TypeScript + Vite)
|
||
- 配置开发环境
|
||
|
||
## 详细步骤
|
||
|
||
### 1. 创建项目根目录结构
|
||
|
||
```bash
|
||
mkdir news-aggregator
|
||
cd news-aggregator
|
||
mkdir backend frontend docs
|
||
```
|
||
|
||
### 2. 初始化后端项目
|
||
|
||
#### 2.1 创建 backend 目录并初始化
|
||
```bash
|
||
cd backend
|
||
npm init -y
|
||
```
|
||
|
||
#### 2.2 安装后端依赖
|
||
|
||
**核心依赖**:
|
||
```bash
|
||
npm install fastify @fastify/cors @fastify/static
|
||
npm install @prisma/client
|
||
npm install openai
|
||
npm install playwright
|
||
npm install node-cron
|
||
npm install dotenv
|
||
```
|
||
|
||
**开发依赖**:
|
||
```bash
|
||
npm install -D typescript @types/node
|
||
npm install -D tsx nodemon
|
||
npm install -D prisma
|
||
npm install -D @types/node-cron
|
||
```
|
||
|
||
#### 2.3 配置 TypeScript
|
||
|
||
创建 `tsconfig.json`:
|
||
```json
|
||
{
|
||
"compilerOptions": {
|
||
"target": "ES2022",
|
||
"module": "commonjs",
|
||
"lib": ["ES2022"],
|
||
"outDir": "./dist",
|
||
"rootDir": "./src",
|
||
"strict": true,
|
||
"esModuleInterop": true,
|
||
"skipLibCheck": true,
|
||
"forceConsistentCasingInFileNames": true,
|
||
"resolveJsonModule": true,
|
||
"moduleResolution": "node",
|
||
"types": ["node"]
|
||
},
|
||
"include": ["src/**/*"],
|
||
"exclude": ["node_modules", "dist"]
|
||
}
|
||
```
|
||
|
||
#### 2.4 配置 package.json scripts
|
||
|
||
```json
|
||
{
|
||
"scripts": {
|
||
"dev": "tsx watch src/app.ts",
|
||
"build": "tsc",
|
||
"start": "node dist/app.js",
|
||
"prisma:generate": "prisma generate",
|
||
"prisma:migrate": "prisma migrate dev",
|
||
"prisma:studio": "prisma studio"
|
||
}
|
||
}
|
||
```
|
||
|
||
#### 2.5 创建基础目录结构
|
||
|
||
```bash
|
||
mkdir -p src/{config,web/{routes,middleware},services,collectors,distributors,utils,types}
|
||
mkdir prisma
|
||
```
|
||
|
||
#### 2.6 创建环境变量文件
|
||
|
||
创建 `.env.example`:
|
||
```env
|
||
# 应用配置
|
||
NODE_ENV=development
|
||
PORT=3000
|
||
|
||
# 数据库配置
|
||
DATABASE_URL="mysql://root:password@localhost:3306/news_aggregator"
|
||
|
||
# 其他配置
|
||
LOG_LEVEL=info
|
||
```
|
||
|
||
创建 `.env`(复制 .env.example 并修改)
|
||
|
||
#### 2.7 创建 .gitignore
|
||
|
||
```
|
||
node_modules/
|
||
dist/
|
||
.env
|
||
*.log
|
||
.DS_Store
|
||
```
|
||
|
||
### 3. 初始化前端项目
|
||
|
||
#### 3.1 使用 Vite 创建 Vue 3 项目
|
||
|
||
```bash
|
||
cd ../frontend
|
||
npm create vite@latest . -- --template vue-ts
|
||
```
|
||
|
||
#### 3.2 安装前端依赖
|
||
|
||
**核心依赖**:
|
||
```bash
|
||
npm install vue-router@4 pinia
|
||
npm install axios
|
||
npm install element-plus
|
||
npm install @element-plus/icons-vue
|
||
```
|
||
|
||
**开发依赖**:
|
||
```bash
|
||
npm install -D @types/node
|
||
```
|
||
|
||
#### 3.3 配置 vite.config.ts
|
||
|
||
```typescript
|
||
import { defineConfig } from 'vite'
|
||
import vue from '@vitejs/plugin-vue'
|
||
import path from 'path'
|
||
|
||
export default defineConfig({
|
||
plugins: [vue()],
|
||
resolve: {
|
||
alias: {
|
||
'@': path.resolve(__dirname, './src')
|
||
}
|
||
},
|
||
server: {
|
||
port: 5173,
|
||
proxy: {
|
||
'/api': {
|
||
target: 'http://localhost:3000',
|
||
changeOrigin: true
|
||
}
|
||
}
|
||
}
|
||
})
|
||
```
|
||
|
||
#### 3.4 配置 tsconfig.json
|
||
|
||
更新 `tsconfig.json`:
|
||
```json
|
||
{
|
||
"compilerOptions": {
|
||
"target": "ES2020",
|
||
"useDefineForClassFields": true,
|
||
"module": "ESNext",
|
||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||
"skipLibCheck": true,
|
||
"moduleResolution": "bundler",
|
||
"allowImportingTsExtensions": true,
|
||
"resolveJsonModule": true,
|
||
"isolatedModules": true,
|
||
"noEmit": true,
|
||
"jsx": "preserve",
|
||
"strict": true,
|
||
"noUnusedLocals": true,
|
||
"noUnusedParameters": true,
|
||
"noFallthroughCasesInSwitch": true,
|
||
"baseUrl": ".",
|
||
"paths": {
|
||
"@/*": ["./src/*"]
|
||
}
|
||
},
|
||
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
|
||
"references": [{ "path": "./tsconfig.node.json" }]
|
||
}
|
||
```
|
||
|
||
#### 3.5 创建基础目录结构
|
||
|
||
```bash
|
||
mkdir -p src/{router,stores,views/{config,reports,articles,monitoring},components,api,types,utils}
|
||
```
|
||
|
||
#### 3.6 配置 Element Plus
|
||
|
||
创建 `src/plugins/element-plus.ts`:
|
||
```typescript
|
||
import ElementPlus from 'element-plus'
|
||
import 'element-plus/dist/index.css'
|
||
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
|
||
import type { App } from 'vue'
|
||
|
||
export function setupElementPlus(app: App) {
|
||
app.use(ElementPlus)
|
||
|
||
// 注册所有图标
|
||
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
||
app.component(key, component)
|
||
}
|
||
}
|
||
```
|
||
|
||
#### 3.7 更新 main.ts
|
||
|
||
```typescript
|
||
import { createApp } from 'vue'
|
||
import { createPinia } from 'pinia'
|
||
import App from './App.vue'
|
||
import router from './router'
|
||
import { setupElementPlus } from './plugins/element-plus'
|
||
|
||
const app = createApp(App)
|
||
const pinia = createPinia()
|
||
|
||
app.use(pinia)
|
||
app.use(router)
|
||
setupElementPlus(app)
|
||
|
||
app.mount('#app')
|
||
```
|
||
|
||
### 4. 创建项目根目录文件
|
||
|
||
#### 4.1 创建 README.md
|
||
|
||
```markdown
|
||
# News Aggregator
|
||
|
||
基于配置的资讯聚合系统,自动收集、整理、推送每日报告。
|
||
|
||
## 快速开始
|
||
|
||
### 环境要求
|
||
- Node.js 20+
|
||
- MySQL 5.6
|
||
- PM2
|
||
|
||
### 安装依赖
|
||
|
||
后端:
|
||
\`\`\`bash
|
||
cd backend
|
||
npm install
|
||
\`\`\`
|
||
|
||
前端:
|
||
\`\`\`bash
|
||
cd frontend
|
||
npm install
|
||
\`\`\`
|
||
|
||
### 配置环境变量
|
||
|
||
复制 `backend/.env.example` 到 `backend/.env` 并修改配置。
|
||
|
||
### 初始化数据库
|
||
|
||
\`\`\`bash
|
||
cd backend
|
||
npm run prisma:migrate
|
||
\`\`\`
|
||
|
||
### 启动开发服务
|
||
|
||
后端:
|
||
\`\`\`bash
|
||
cd backend
|
||
npm run dev
|
||
\`\`\`
|
||
|
||
前端:
|
||
\`\`\`bash
|
||
cd frontend
|
||
npm run dev
|
||
\`\`\`
|
||
|
||
## 文档
|
||
|
||
详细文档请查看 `docs/` 目录。
|
||
|
||
## 技术栈
|
||
|
||
- 后端:Node.js + TypeScript + Fastify + Prisma
|
||
- 前端:Vue 3 + TypeScript + Vite + Element Plus
|
||
- 数据库:MySQL 5.6
|
||
- 部署:PM2
|
||
```
|
||
|
||
#### 4.2 创建 .gitignore(根目录)
|
||
|
||
```
|
||
# 依赖
|
||
node_modules/
|
||
|
||
# 构建产物
|
||
dist/
|
||
build/
|
||
|
||
# 环境变量
|
||
.env
|
||
.env.local
|
||
|
||
# 日志
|
||
*.log
|
||
logs/
|
||
|
||
# 编辑器
|
||
.vscode/
|
||
.idea/
|
||
*.swp
|
||
*.swo
|
||
|
||
# 系统文件
|
||
.DS_Store
|
||
Thumbs.db
|
||
|
||
# PM2
|
||
.pm2/
|
||
```
|
||
|
||
### 5. 初始化 Git 仓库
|
||
|
||
```bash
|
||
cd .. # 回到项目根目录
|
||
git init
|
||
git add .
|
||
git commit -m "Initial commit: project setup"
|
||
```
|
||
|
||
## 验证步骤
|
||
|
||
### 1. 验证后端项目
|
||
```bash
|
||
cd backend
|
||
npm run dev
|
||
```
|
||
应该能看到 TypeScript 编译成功(即使还没有代码)。
|
||
|
||
### 2. 验证前端项目
|
||
```bash
|
||
cd frontend
|
||
npm run dev
|
||
```
|
||
应该能在浏览器访问 http://localhost:5173 看到 Vue 默认页面。
|
||
|
||
### 3. 验证目录结构
|
||
```bash
|
||
tree -L 3 -I node_modules
|
||
```
|
||
应该看到完整的目录结构。
|
||
|
||
## 预期产物
|
||
|
||
- ✅ 项目根目录结构创建完成
|
||
- ✅ 后端项目初始化完成,依赖安装完成
|
||
- ✅ 前端项目初始化完成,依赖安装完成
|
||
- ✅ TypeScript 配置完成
|
||
- ✅ 开发环境配置完成
|
||
- ✅ Git 仓库初始化完成
|
||
|
||
## 下一步
|
||
|
||
完成本任务后,继续 [任务 02:数据库搭建](./02-database-setup.md)。
|