-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (49 loc) · 1.6 KB
/
server.js
File metadata and controls
62 lines (49 loc) · 1.6 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
50
51
52
53
54
55
56
57
58
59
60
61
const express = require("express");
const path = require("path");
const fetch = require("node-fetch");
const app = express();
const PORT = process.env.PORT || 3000;
const NYCKEL_FUNCTION_ID = process.env.NYCKEL_FUNCTION_ID || "blurry-image-identifier";
const NYCKEL_ACCESS_TOKEN = process.env.NYCKEL_ACCESS_TOKEN;
app.use(express.json({ limit: "5mb" }));
app.use(express.static(__dirname));
app.post("/api/blurry-image", async (req, res) => {
if (!NYCKEL_ACCESS_TOKEN) {
return res.status(500).json({
error: "NYCKEL_ACCESS_TOKEN is not configured on the server"
});
}
const data = req.body && req.body.data;
if (!data) {
return res.status(400).json({
error: 'Missing "data" field. Provide an image URL or data URI.'
});
}
const url = "https://www.nyckel.com/v1/functions/" + NYCKEL_FUNCTION_ID + "/invoke";
try {
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + NYCKEL_ACCESS_TOKEN,
"Content-Type": "application/json"
},
body: JSON.stringify({ data: data })
});
const json = await response.json();
if (!response.ok) {
return res.status(response.status).json({
error: json && json.message ? json.message : "Nyckel API error",
details: json
});
}
return res.json(json);
} catch (error) {
return res.status(500).json({
error: "Failed to call Nyckel API",
details: error && error.message ? error.message : String(error)
});
}
});
app.listen(PORT, () => {
console.log("BlurYEffect server listening on http://localhost:" + PORT);
});