-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (48 loc) · 1.31 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
const path = require("path");
const axios = require("axios");
const express = require("express");
const bodyParser = require("body-parser");
require("dotenv").config();
// Set up express.
const app = express();
const port = process.env.PORT || 9000;
app.use(bodyParser.json({ limit: "50mb" }));
app.use(express.static(path.join(__dirname, "public")));
const authenticate = async () => {
try {
const response = await axios({
method: "post",
url: "https://iam.bluemix.net/oidc/token",
auth: {
username: "bx",
password: "bx"
},
data: `apikey=${process.env.APIKEY}&grant_type=urn:ibm:params:oauth:grant-type:apikey`
});
return response.data;
} catch (error) {
console.error(error.response.data);
}
};
app.post("/api/detect", async (req, res) => {
const { access_token } = await authenticate();
try {
const response = await axios({
method: "post",
url: process.env.SCORING_ENDPOINT,
headers: {
Authorization: `Bearer ${access_token}`
},
data: {
values: [req.body]
}
});
res.send(response.data);
} catch (error) {
console.error(error.response.data);
res.end();
}
});
app.listen(port, () => {
console.log("listening on port " + port);
});