Skip to content

Commit

Permalink
🥙重写路由 #25 #22 #21 #17 #14 #5 #3
Browse files Browse the repository at this point in the history
1、让路由文件更加简洁
2、错误处理中间件
  • Loading branch information
Johnserf-Seed committed Sep 3, 2023
1 parent 6fec6dd commit d86172e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ router.get('/', async function(req, res, next) {
dyCookie = null;
}

// 如果dycookie不存在或无法解析rs
const allEmpty = Object.values(dyCookie).every(value => value === '');
if (allEmpty) {
res.render('index', { videoData: {work:false} });
return;
}

// 如果未提供URL,则使用默认视频
let videoUrl = req.query.url;
if (!videoUrl) {
videoUrl = 'https://v.douyin.com/NKyY6Ch/';
}

// 检查URL的有效性(简单的)
if (!videoUrl.startsWith('https://v.douyin.com/') && !videoUrl.startsWith('https://www.douyin.com/')) {
throw new Error('无效的URL地址');
}

// 根据提供的URL获取视频ID
const videoId = await GetID(videoUrl);

// 使用视频ID和cookie获取视频详情
const videoData = await GetInfo(videoId, dyCookie, getXB);
Expand All @@ -31,6 +44,17 @@ router.get('/', async function(req, res, next) {
}
});

// 错误处理中间件
router.use((err, req, res, next) => {
console.error(err.stack)
// 不要尝试嗅探并覆盖响应的MIME类型
res.setHeader('X-Content-Type-Options', 'nosniff');
// 根据错误类型或消息为用户提供不同的反馈
if (err.message.includes('无效的URL地址')) {
res.status(400).render('error', { message: '提供的URL无效' });
} else {
res.status(500).render('error', { message: '服务器内部错误' });
}
});

module.exports = router;

0 comments on commit d86172e

Please sign in to comment.