This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (37 loc) · 1.26 KB
/
Copy pathserver.js
File metadata and controls
49 lines (37 loc) · 1.26 KB
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
'use strict';
const express = require('express');
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const PORT = 8080;
const HOST = '0.0.0.0';
const LT_VERSION = '2018-05-01';
const LT_URL = process.env.lt_url || 'https://gateway.watsonplatform.net/language-translator/api';
//const PORT = process.env.PORT || 3000;
const languageTranslator = new LanguageTranslatorV3({
version: LT_VERSION,
iam_apikey: process.env.lt_key,
url: LT_URL,
});
const app = express();
app.get('/translate', (req, res) => {
if(!req.query.text) {
res.send('You must pass in the text to translate with the url using ?text=text!\n');
}
if(!req.query.lang) {
console.log('No language passed to translate to. Converting to Spanish by default.');
}
const translateParams = {
text: req.query.text,
model_id: req.query.lang ? req.query.lang : 'en-es',
};
languageTranslator.translate(translateParams)
.then(translationResult => {
console.log(JSON.stringify(translationResult, null, 2));
res.send(JSON.stringify(translationResult, null, 2));
})
.catch(err => {
console.log('error:', err);
res.send('Something went wrong!\n');
});
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);