-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (27 loc) · 799 Bytes
/
server.js
File metadata and controls
34 lines (27 loc) · 799 Bytes
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
34
import express from 'express';
import bodyParser from 'body-parser';
import { translate } from './translate.js';
const token = process.env.FANYI_TOKEN;
const app = express();
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.post('/', async (req, res) => {
if (req.get('x-fanyi-token') !== token) {
return res.status(401).json({ message: 'Forbidden' });
}
const { text } = req.body;
if (typeof text !== 'string') {
return res.status(400).json({ message: 'text is not a string' });
}
try {
const result = await translate(text);
res.json(result);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
app.listen(process.env.LEANCLOUD_APP_PORT || 3000, () => {
console.log('App Launched');
});