Update server.js

This commit is contained in:
CxJuice 2023-10-13 22:57:40 +08:00 committed by GitHub
parent 36fa2e90e7
commit 2df787bc9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,23 @@ const PORT = process.env.PORT || 3000;
const JSON_FOLDER_PATH = path.join(__dirname, 'json'); const JSON_FOLDER_PATH = path.join(__dirname, 'json');
const LOCALES_JSON_FOLDER_PATH = path.join(__dirname, 'json/locales'); 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 文件 // 获取文件夹内的所有 JSON 文件
app.get('/json-files', (req, res) => { app.get('/json-files', (req, res) => {
fs.readdir(JSON_FOLDER_PATH, (err, files) => { fs.readdir(JSON_FOLDER_PATH, (err, files) => {
@ -31,14 +48,13 @@ app.get('/json-files/:filename', (req, res) => {
} }
try { try {
const jsonData = JSON.parse(data); const jsonData = JSON.parse(data);
res.json(jsonData); res.json(transformToLowerCase(jsonData));
} catch (parseErr) { } catch (parseErr) {
res.status(500).send('Error parsing JSON.'); res.status(500).send('Error parsing JSON.');
} }
}); });
}); });
// TODO: 新版本兼容@Fierce-Cat
// 获取locales目录下的特定的 JSON 文件 // 获取locales目录下的特定的 JSON 文件
app.get('/json-files/locales/:filename', (req, res) => { app.get('/json-files/locales/:filename', (req, res) => {
const filePath = path.join(LOCALES_JSON_FOLDER_PATH, req.params.filename); const filePath = path.join(LOCALES_JSON_FOLDER_PATH, req.params.filename);
@ -52,7 +68,7 @@ app.get('/json-files/locales/:filename', (req, res) => {
} }
try { try {
const jsonData = JSON.parse(data); const jsonData = JSON.parse(data);
res.json(jsonData); res.json(transformToLowerCase(jsonData));
} catch (parseErr) { } catch (parseErr) {
res.status(500).send('Error parsing JSON.'); res.status(500).send('Error parsing JSON.');
} }