-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
56 lines (54 loc) · 1.67 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
const http = require("http");
const fs = require("fs");
const clarifai = require("clarifai");
const app = new clarifai.App({
apiKey: "b8f4339020aa4524bdf092820a486c3f"
});
http.createServer(function(request, response) {
if (request.url == "/") {
fs.readFile("test-client.html", (error, data) => {
response.writeHead("200", {"Content-Type": "text/html"});
response.write(data);
response.end();
});
} else if (request.url == "/clarifai") {
if (request.method == "POST") {
var body = "";
request.on("data", data => {
body += data;
if (body.length > 1e9) {
request.connection.destroy();
}
});
request.on("end", () => {
var data = JSON.parse(body).image;
app.models.predict(clarifai.GENERAL_MODEL, {base64: data}).then(
predictions => {
var concepts = predictions.outputs[0].data.concepts;
response.writeHead("200", {"Content-Type": "application/json"});
response.write(
JSON.stringify(
(concepts.filter(a => a.value > 0.9)
|| [concepts[0]]).map(a => a.name)
)
);
response.end();
},
error => {
console.log(error);
}
);
});
} else {
response.writeHead("405", {"Content-Type": "text/html"});
response.write("405 Method Not Allowed");
response.end();
}
} else if (request.url == "/jquery") {
fs.readFile("jquery-3.3.1.min.js", (error, data) => {
response.writeHead("200", {"Content-Type": "text/javascript"});
response.write(data);
response.end();
})
}
}).listen(5000);