From 837e0f65ad07996defa4990007751f315491609b Mon Sep 17 00:00:00 2001 From: CxJuice <110189934+CxJuice@users.noreply.github.com> Date: Sat, 16 Dec 2023 20:14:52 +0800 Subject: [PATCH] Update index.js --- index.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/index.js b/index.js index 85d9c62..6d1637e 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,37 @@ function transformKeysToLowerCase(obj) { return newObj; } +// 获取文件夹内的所有 JSON 文件 +app.get('/json-files', (req, res) => { + fs.readdir(JSON_FOLDER_PATH, (err, files) => { + if (err) { + return res.status(500).send('Server error.'); + } + const jsonFiles = files.filter(file => file.endsWith('.json')); + res.json(jsonFiles); + }); +}); + +// 获取特定的 JSON 文件 +app.get('/json-files/:filename', (req, res) => { + const filePath = path.join(JSON_FOLDER_PATH, req.params.filename); + + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + if (err.code === 'ENOENT') { + return res.status(404).send('File not found.'); + } + return res.status(500).send('Server error.'); + } + try { + const jsonData = JSON.parse(data); + res.json(transformKeysToLowerCase(jsonData)); + } catch (parseErr) { + res.status(500).send('Error parsing JSON.'); + } + }); +}); + // 获取locales目录下的特定的 JSON 文件 app.get('/json-files/locales/:filename', (req, res) => { const filePath = path.join(LOCALES_JSON_FOLDER_PATH, req.params.filename);