-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (50 loc) · 1.53 KB
/
server.js
File metadata and controls
59 lines (50 loc) · 1.53 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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors"); // Import the CORS middleware
const app = express();
const PORT = 3000;
const API_KEY = "AIzaSyB4tMC8eww1IC8pHBjzt9JvrrJ5e8sjHGg";
// Enable CORS for all origins
app.use(cors());
// Middleware to parse JSON requests
app.use(bodyParser.json());
// Route to handle image analysis
app.post("/analyze", async (req, res) => {
const { base64Image } = req.body;
if (!base64Image) {
return res.status(400).json({ error: "Base64 image is required" });
}
try {
const response = await fetch(
`https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
requests: [
{
image: { content: base64Image },
features: [{ type: "LABEL_DETECTION", maxResults: 5 }],
},
],
}),
}
);
if (!response.ok) {
const errorText = await response.text();
console.error("Google Vision API Error:", errorText);
return res.status(response.status).json({ error: errorText });
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error during Vision API call:", error);
res.status(500).json({ error: "Failed to analyze the image" });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});