-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (40 loc) · 1.33 KB
/
server.js
File metadata and controls
49 lines (40 loc) · 1.33 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
const express = require("express");
const fetch = require("node-fetch");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = "YOUR_API_KEY_HERE_GEMINI";
app.use(cors());
app.use(express.json());
app.get("/", (req, res) => {
res.send(" Gemini Proxy is running");
});
app.post("/ask", async (req, res) => {
try {
const userText = req.body.text;
if (!userText) {
return res.status(400).json({ answer: " No input text provided" });
}
const geminiPayload = {
contents: [
{
parts: [{ text: userText }]
}
]
};
const response = await fetch(`https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key=${API_KEY}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(geminiPayload)
});
const data = await response.json();
const answer = data?.candidates?.[0]?.content?.parts?.[0]?.text || " No answer available.";
res.json({ answer });
} catch (err) {
console.error(" Proxy error:", err.message);
res.status(500).json({ answer: "Server Error: " + err.message });
}
});
app.listen(PORT, "0.0.0.0", () => {
console.log(`Gemini Proxy listening on http://localhost:${PORT}`);
});