-
Notifications
You must be signed in to change notification settings - Fork 30
/
server.js
33 lines (27 loc) · 866 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const express = require('express');
const bodyParser = require('body-parser');
const { translate } = require('./translate');
const app = express();
const PORT = 9000;
app.use(bodyParser.json());
app.post('/translate', async (req, res) => {
const { text, source_lang, target_lang } = req.body;
try {
const result = await translate(text, source_lang, target_lang);
const responseData = {
alternatives: result.alternatives,
code: 200,
data: result.text, // 取第一个翻译结果
id: Math.floor(Math.random() * 10000000000), // 生成一个随机 ID
method: 'Free',
source_lang,
target_lang,
};
res.json(responseData);
} catch (error) {
res.status(500).json({ error: 'Translation failed' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});