mirror of
https://mirror.ghproxy.com/https://github.com/CxJuice/ScWeb_Chinese_Translate.git
synced 2024-12-23 00:23:44 +08:00
Update server.js
This commit is contained in:
parent
36fa2e90e7
commit
2df787bc9f
22
server.js
22
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.');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user