-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (52 loc) · 1.79 KB
/
Copy pathserver.js
File metadata and controls
66 lines (52 loc) · 1.79 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
62
63
64
65
66
import express from "express";
import fetch from "node-fetch";
import dotenv from "dotenv";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
dotenv.config();
// Dosya yollarını tanımlıyoruz (ES Module ayarı)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors());
app.use(express.json());
// 1. ÖNEMLİ: Ana dizindeki dosyaları (index.html, assets vs.) dışarıya aç
// index.html senin ana klasöründe olduğu için __dirname kullanıyoruz.
app.use(express.static(__dirname));
// Google Gemini API URL
const API_URL_BASE = "https://generativelanguage.googleapis.com/v1beta/models/";
// API Endpoint
app.post("/api/request", async (req, res) => {
const { model, input } = req.body;
const targetModel = model || "gemini-1.5-flash";
try {
const response = await fetch(`${API_URL_BASE}${targetModel}:generateContent?key=${process.env.API_KEY}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
contents: [{
parts: [{ text: input }]
}]
})
});
const data = await response.json();
if (data.error) {
console.error("Google API Hatası:", data.error);
return res.status(400).json(data);
}
res.json(data);
} catch (err) {
console.error("Sunucu Hatası:", err);
res.status(500).json({ error: err.message });
}
});
// 2. ÖNEMLİ: Siteye giren herkesi index.html'e yönlendir
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// 3. ÖNEMLİ: Render'ın verdiği portu kullan, yoksa 3000'i kullan
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));