-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (52 loc) · 1.57 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require("express"),
app = express(),
port = process.env.PORT || 5000,
cors = require("cors");
app.use(cors());
app.listen(port, () => console.log("Backend server live on " + port));
const ToneAnalyzerV3 = require("ibm-watson/tone-analyzer/v3");
const { IamAuthenticator } = require("ibm-watson/auth");
const toneAnalyzer = new ToneAnalyzerV3({
version: "2017-09-21",
authenticator: new IamAuthenticator({
apikey: "0ElAfANDGaue9dFMsTMutYaxAMO6l1_OwvON9tE-yxl3",
}),
serviceUrl:
"https://api.us-south.tone-analyzer.watson.cloud.ibm.com/instances/a7465f8e-2d06-40ab-9eb4-f31f9b1deb83",
});
// const text =
// "Team, I know that times are tough! Product " +
// "sales have been disappointing for the past three " +
// "quarters. We have a competitive product, but we " +
// "need to do a better job of selling it!";
// const toneParams = {
// toneInput: { text: text },
// contentType: "application/json",
// };
// toneAnalyzer
// .tone(toneParams)
// .then((toneAnalysis) => {
// console.log(JSON.stringify(toneAnalysis, null, 2));
// })
// .catch((err) => {
// console.log("error:", err);
// });
app.get("/mood/:text", function (req, res) {
let text = req.params.text;
console.log(text);
let toneParams = {
toneInput: { text: text },
contentType: "application/json",
};
toneAnalyzer
.tone(toneParams)
.then((toneAnalysis) => {
res.send(toneAnalysis.result);
})
.catch((err) => {
console.log("error:", err);
});
});
app.get("/", (req, res) => {
res.send({ message: "We did it!" });
});