forked from ilirt123/ECE528-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
43 lines (35 loc) · 1.14 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
const express = require("express");
const bodyParser = require("body-parser");
const { exec } = require("child_process");
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(express.static("public"));
app.post("/predict", (req, res) => {
const endpointId = "6567598456985092096";
const projectId = "my-project-1-ece-528";
const inputData = req.body;
const command = `curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${projectId}/locations/us-central1/endpoints/${endpointId}:predict \
-d '${JSON.stringify(inputData)}'`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
res.status(500).send("Error");
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
res.status(500).send("Error");
return;
}
const result = JSON.parse(stdout);
res.send(result);
});
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});