import express from "express"; import fetch from "node-fetch"; // html.cafeの環境によっては fetch が標準で使える場合もあります import cors from "cors"; const app = express(); const PORT = 3000; // ブラウザ側からのアクセスを許可 app.use(cors()); // GET /proxy?url=対象URL でプロキシ app.get("/proxy", async (req, res) => { const targetUrl = req.query.url; if (!targetUrl) { return res.status(400).send("urlパラメータが必要です"); } try { // サーバー側で対象URLにアクセス const response = await fetch(targetUrl, { headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", // ブロック回避用 }, }); const contentType = response.headers.get("content-type"); res.setHeader("Content-Type", contentType); const body = await response.text(); res.send(body); } catch (err) { res.status(500).send("プロキシ失敗: " + err.message); } }); app.listen(PORT, () => { console.log(`プロキシサーバー起動: http://localhost:${PORT}/proxy?url=対象URL`); });