Follow our Instagram channel
Posts

/** * Full AI Chat App (Frontend + Backend combined) * Run: node server.js * Visit: http://localhost:3000 * WARNING: API key is exposed in frontend (unsafe for public) */ const express = require("express"); const fetch = require("node-fetch"); // npm i node-fetch@2 const path = require("path"); const app = express(); app.use(express.json()); // === CONFIG === const PORT = 3000; const API_KEY = "AIzaSyAdP8PjI57ifXhHMxe3NWrLHJ57a4dVuVo"; // your API key (unsafe) const MODEL = "models/gemini-pro"; const GOOGLE_URL = `https://generativelanguage.googleapis.com/v1beta/${MODEL}:generateContent?key=${API_KEY}`; // Serve static HTML frontend app.get("/", (req, res)=>{ res.send(` Problem Solver AI
πŸ€– Problem Solver AI
πŸ€–
Hello! Ask me anything 😊
`); }); // Backend endpoint to call Google AI app.post("/solve", async (req, res)=>{ try{ const { prompt } = req.body; if(!prompt) return res.status(400).json({error:"Prompt required"}); const body = { contents:[{ parts:[{ text: prompt }] }] }; const response = await fetch(GOOGLE_URL,{ method:"POST", headers:{ "Content-Type":"application/json" }, body: JSON.stringify(body) }); const data = await response.json(); let answer="No response"; if(data?.candidates?.[0]?.content?.parts?.[0]?.text){ answer=data.candidates[0].content.parts[0].text; } else if(data?.candidates?.[0]?.output){ answer=data.candidates[0].output; } else { answer=JSON.stringify(data,null,2); } res.json({answer}); }catch(err){ res.status(500).json({error:err.message}); } }); app.listen(PORT, ()=>console.log("✅ Server running at http://localhost:"+PORT));

Post a Comment

NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...