-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (42 loc) · 1.22 KB
/
app.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
40
41
42
43
44
45
46
47
48
49
50
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
require('dotenv').config();
app.use(bodyParser.json());
app.use(express.json());
app.use(express.static('./public'));
const languageTranslator = new LanguageTranslatorV3({
iam_apikey: process.env.API_KEY,
url: process.env.URL,
version: process.env.VERSION
});
app.post('/translate', (req, res) => {
const { text, fromLanguage, toLanguage } = req.body;
const params = {
text: text,
source: fromLanguage,
target: toLanguage
};
languageTranslator.translate(params, (err, response) => {
if (err) {
console.error(err);
res.status(500).json(err);
} else {
res.json(response);
}
});
});
app.post('/identify', (req, res) => {
const { text } = req.body;
const params = { text: text };
languageTranslator.identify(params, (err, response) => {
if (err) {
console.error(err);
res.status(500).json(err);
} else {
res.json(response);
}
});
})
app.listen(3000, () => console.log(`Running`));