diff --git a/backend/routes/index.js b/backend/routes/index.js index fa55854..27a8c87 100644 --- a/backend/routes/index.js +++ b/backend/routes/index.js @@ -6,11 +6,20 @@ const router = express.Router(); const modifyLink = require('../services/modifyLink'); +const cachedResult = new Map(); + router.get('/', async (req, res) => { try { const url = urlencode.decode(req.query.url); if (!url) return res.status(400).end('URL을 파라미터에 포함해야 합니다.'); if (!isUrl(url)) return res.status(400).end('잘못된 형식의 URL입니다.'); + + // cache hit + if (cachedResult.get(url)) { + return res.end(cachedResult.get(url)); + } + + // cache miss const result = await axios.get(url, { headers: { connection: 'keep-alive', @@ -22,7 +31,10 @@ router.get('/', async (req, res) => { const html = result.data; if (!html) return res.status(404).end('HTML을 받아오지 못했습니다.'); + // caching const modified_html = modifyLink(url, html); + cachedResult.set(url, modified_html); + return res.end(modified_html); } catch (e) { return res.status(500).end('알 수 없는 오류가 발생했습니다.');