FPGA_WebLab/server/src/TutorialService.ts

31 lines
967 B
TypeScript

// 此接口提供获取例程目录服务
// GET /api/tutorials 返回所有可用的例程目录
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { Request, Response } from 'express';
// 获取当前文件的目录
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicDir = path.resolve(__dirname, '../public');
export function getTutorials(req: Request, res: Response) {
try {
const docDir = path.join(publicDir, 'doc');
// 读取doc目录下的所有文件夹
const entries = fs.readdirSync(docDir, { withFileTypes: true });
const dirs = entries
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
// 返回文件夹列表
res.json({ tutorials: dirs });
} catch (error) {
console.error('获取例程目录失败:', error);
res.status(500).json({ error: '无法读取例程目录' });
}
}