-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (48 loc) · 1.24 KB
/
Copy pathserver.js
File metadata and controls
57 lines (48 loc) · 1.24 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
import express from "express";
import cors from "cors";
import "dotenv/config";
import { GoogleGenAI } from "@google/genai";
const app = express();
const PORT = 5000;
// middleware
app.use(cors());
app.use(express.json());
// Gemini client (SERVER ONLY)
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY,
});
// route
app.post("/api/movies", async (req, res) => {
try {
const { query } = req.body;
if (!query) {
return res.status(400).json({ error: "Query is required" });
}
// const models = await ai.models.list();
// console.log(models);
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: `
List 5 popular movies that match the following query:
"${query}"
Rules:
- Only movie names
- One per line
- No explanation
- Prefer mainstream films
`,
});
const movies = response.text
.split("\n")
.map(m => m.trim())
.filter(Boolean);
res.json({ movies });
} catch (err) {
console.error(err);
res.status(500).json({ error: "Gemini request failed" });
}
});
// start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});