forked from axipo/pdfTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (33 loc) · 1.25 KB
/
index.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
34
35
36
37
38
39
const express = require('express');
const app = express();
const path = require('path');
const open = require('open');
const { youdao, baidu, google } = require('translation.js')
//parameter
const port = 3000;
app.use(express.static(path.resolve(__dirname)));
app.use(express.json());
// app.use(express.static(path.resolve(__dirname, 'build')));
app.get('/', (req, res) => res.redirect(301, '/web/viewer.html'));
app.post('/query', (req, res) => {
google.translate({
text: req.body.word,
to: 'zh-CN'
}).then(result => {
res.send(result.result.join(""));
console.log(result.result) // result 的数据结构见下文
})
// translate(req.query.word, {to: 'zh-cn'}).then(res => {
// res.send(res.text);
// console.log(res.text);
// }).catch(err => {
// res.status(400).send('请求失败');
// console.error(err);
// });
})
app.listen(port, () => {
console.log(`app listening on port ${port}!`);
console.log('程序将自动打开浏览器,注意:目前仅仅测试了chrome,请尽量使用基于chrome的浏览器')
console.log('如果您的默认浏览器是ie,可以复制网址到其他浏览器打开(推荐chrome)\n')
})
open('http://127.0.0.1:3000');