From 2df787bc9ff0fb7c899a7e70c6d45e68de25d831 Mon Sep 17 00:00:00 2001 From: CxJuice <110189934+CxJuice@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:57:40 +0800 Subject: [PATCH] Update server.js --- server.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index c54340c..0034b2d 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,23 @@ const PORT = process.env.PORT || 3000; const JSON_FOLDER_PATH = path.join(__dirname, 'json'); const LOCALES_JSON_FOLDER_PATH = path.join(__dirname, 'json/locales'); +function transformToLowerCase(obj) { + if (typeof obj !== 'object' || obj === null) { + return obj; + } + + if (Array.isArray(obj)) { + return obj.map(value => transformToLowerCase(value)); + } + + let newObj = {}; + for (let key in obj) { + newObj[key.toLowerCase()] = (typeof obj[key] === 'string') ? obj[key].toLowerCase() : transformToLowerCase(obj[key]); + } + + return newObj; +} + // 获取文件夹内的所有 JSON 文件 app.get('/json-files', (req, res) => { fs.readdir(JSON_FOLDER_PATH, (err, files) => { @@ -31,14 +48,13 @@ app.get('/json-files/:filename', (req, res) => { } try { const jsonData = JSON.parse(data); - res.json(jsonData); + res.json(transformToLowerCase(jsonData)); } catch (parseErr) { res.status(500).send('Error parsing JSON.'); } }); }); -// TODO: 新版本兼容@Fierce-Cat // 获取locales目录下的特定的 JSON 文件 app.get('/json-files/locales/:filename', (req, res) => { const filePath = path.join(LOCALES_JSON_FOLDER_PATH, req.params.filename); @@ -52,7 +68,7 @@ app.get('/json-files/locales/:filename', (req, res) => { } try { const jsonData = JSON.parse(data); - res.json(jsonData); + res.json(transformToLowerCase(jsonData)); } catch (parseErr) { res.status(500).send('Error parsing JSON.'); }