import { Router } from 'express'; import fs from 'fs'; import path from 'path'; import { ROOT } from '../rootDir'; const router = Router(); const LOCALES_DIR = path.join( ROOT, 'source', 'locale-data' ); router.get('/:locale/*', (req, res) => { const locale = req.params.locale; const filePath = (req.params as Record)[0]; const abs = path.resolve(path.join(LOCALES_DIR, locale, filePath)); if (!abs.startsWith(path.resolve(LOCALES_DIR) + path.sep)) { res.status(403).json({ error: 'Forbidden' }); return; } if (!fs.existsSync(abs)) { res.status(404).json({ error: 'Not found' }); return; } const ext = path.extname(abs).slice(1); if (ext === 'html') res.type('text/html').send(fs.readFileSync(abs, 'utf8')); else if (ext === 'json') res.json(JSON.parse(fs.readFileSync(abs, 'utf8'))); else res.type('text/plain').send(fs.readFileSync(abs, 'utf8')); }); export default router;