代码片段
约 109 字小于 1 分钟
2025-11-11
返回脚本文件
@Get('script/xxxx.js')
@Header('Content-Type', 'application/javascript; charset=utf-8')
async getInjectAd(@Res() res: Response) {
const filePath = path.join(process.cwd(), 'script', 'xxxx.js');
try {
const content = await fs.readFile(filePath, 'utf8');
res.send(content);
} catch (err) {
res.status(404).send('script/inject-ad.js not found');
}
}跨域解决
app.enableCors({
origin: '*', // 允许所有来源
// origin: [
// 'http://localhost:5173',
// 'http://127.0.0.1:5173',
// ],
credentials: true, // 如果前端未来需要携带 Cookie 或认证信息
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
});