2023-10-04 23:08:15 +08:00
|
|
|
const express = require('express');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
const JSON_FOLDER_PATH = path.join(__dirname, 'json');
|
2023-10-10 10:16:45 +08:00
|
|
|
const LOCALES_JSON_FOLDER_PATH = path.join(__dirname, 'json/locales');
|
2023-10-04 23:08:15 +08:00
|
|
|
|
2023-10-13 23:23:52 +08:00
|
|
|
function transformKeysToLowerCase(obj) {
|
2023-10-13 22:57:40 +08:00
|
|
|
if (typeof obj !== 'object' || obj === null) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
2023-10-13 23:23:52 +08:00
|
|
|
return obj.map(value => transformKeysToLowerCase(value));
|
2023-10-13 22:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let newObj = {};
|
|
|
|
for (let key in obj) {
|
2023-10-13 23:23:52 +08:00
|
|
|
newObj[key.toLowerCase()] = transformKeysToLowerCase(obj[key]);
|
2023-10-13 22:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
|
2023-10-04 23:08:15 +08:00
|
|
|
// 获取文件夹内的所有 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);
|
2023-10-13 23:23:52 +08:00
|
|
|
res.json(transformKeysToLowerCase(jsonData));
|
2023-10-04 23:08:15 +08:00
|
|
|
} catch (parseErr) {
|
|
|
|
res.status(500).send('Error parsing JSON.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-10-10 10:16:45 +08:00
|
|
|
// 获取locales目录下的特定的 JSON 文件
|
|
|
|
app.get('/json-files/locales/:filename', (req, res) => {
|
|
|
|
const filePath = path.join(LOCALES_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);
|
2023-10-13 23:23:52 +08:00
|
|
|
res.json(transformKeysToLowerCase(jsonData));
|
2023-10-10 10:16:45 +08:00
|
|
|
} catch (parseErr) {
|
|
|
|
res.status(500).send('Error parsing JSON.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-10-04 23:08:15 +08:00
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Server is running on port ${PORT}`);
|
2023-10-06 02:38:53 +08:00
|
|
|
});
|