mirror of
https://mirror.ghproxy.com/https://github.com/CxJuice/ScWeb_Chinese_Translate.git
synced 2024-12-23 01:33:45 +08:00
Update server.js with new locales files
This commit is contained in:
parent
ec2870efec
commit
776c5919b2
22
server.js
22
server.js
@ -5,6 +5,7 @@ const path = require('path');
|
|||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
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');
|
||||||
|
|
||||||
// 获取文件夹内的所有 JSON 文件
|
// 获取文件夹内的所有 JSON 文件
|
||||||
app.get('/json-files', (req, res) => {
|
app.get('/json-files', (req, res) => {
|
||||||
@ -37,6 +38,27 @@ app.get('/json-files/:filename', (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: 新版本兼容@Fierce-Cat
|
||||||
|
// 获取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);
|
||||||
|
res.json(jsonData);
|
||||||
|
} catch (parseErr) {
|
||||||
|
res.status(500).send('Error parsing JSON.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server is running on port ${PORT}`);
|
console.log(`Server is running on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user