-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
177 lines (152 loc) · 5.77 KB
/
Copy pathapp.js
File metadata and controls
177 lines (152 loc) · 5.77 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// app.js
const express = require("express");
const path = require("path");
require("dotenv").config();
const { GoogleGenAI } = require("@google/genai");
const app = express();
// Middleware
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Homepage
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "Homepage.html"));
});
// Main page
app.get("/mainpage", (req, res) => {
res.sendFile(path.join(__dirname, "public", "Mainpage.html"));
});
/**
* Generate fallback roadmap for technical or non-technical goals
*/
function generateFallbackRoadmap(goal) {
const baseSteps = [
{
title: "Understand the Basics",
description: `Learn and master the fundamental concepts of ${goal}.`,
duration: `${2 + Math.floor(Math.random() * 3)} weeks`,
},
{
title: "Explore Intermediate Topics",
description: `Dive deeper into ${goal} concepts and techniques to build a solid foundation.`,
duration: `${3 + Math.floor(Math.random() * 3)} weeks`,
},
{
title: "Practice & Apply",
description: `Work on exercises, mini-projects, or practical tasks to apply your ${goal} knowledge.`,
duration: `${3 + Math.floor(Math.random() * 4)} weeks`,
},
{
title: "Create a Portfolio",
description: `Build real-world projects, presentations, or reports to showcase your skills in ${goal}.`,
duration: `${4 + Math.floor(Math.random() * 4)} weeks`,
},
{
title: "Review & Expand",
description: `Evaluate progress, refine skills, and explore advanced resources to master ${goal}.`,
duration: `${2 + Math.floor(Math.random() * 3)} weeks`,
},
];
return baseSteps.map((step, idx) => ({ id: idx + 1, ...step }));
}
/**
* Gemini-based Learning Roadmap Endpoint
*/
app.post("/api/roadmap", async (req, res) => {
const { goal } = req.body;
if (!goal) {
return res.status(400).json({ error: "Goal is required" });
}
// Fallback roadmap (NO LINKS, NO RESOURCES)
function generateFallbackRoadmap(goal) {
const steps = [
{
title: "Understand the Fundamentals",
description: `Learn the basic concepts and foundations of ${goal}.`,
duration: `${2 + Math.floor(Math.random() * 3)} weeks`
},
{
title: "Build Core Knowledge",
description: `Strengthen your understanding by exploring core areas of ${goal}.`,
duration: `${3 + Math.floor(Math.random() * 3)} weeks`
},
{
title: "Apply What You Learn",
description: `Practice ${goal} through exercises, tasks, or hands-on activities.`,
duration: `${3 + Math.floor(Math.random() * 4)} weeks`
},
{
title: "Create Real-World Work",
description: `Build projects, case studies, or real examples related to ${goal}.`,
duration: `${4 + Math.floor(Math.random() * 4)} weeks`
},
{
title: "Refine & Advance",
description: `Improve your skills, fix gaps, and explore advanced concepts in ${goal}.`,
duration: `${2 + Math.floor(Math.random() * 3)} weeks`
}
];
return steps.map((step, idx) => ({
id: idx + 1,
...step
}));
}
const fallbackRoadmap = generateFallbackRoadmap(goal);
try {
const apiKey = process.env.GEMINI_API_KEY?.trim();
// If no API key → fallback only
if (!apiKey) {
return res.json({ roadmap: fallbackRoadmap });
}
const gemini = new GoogleGenAI({ apiKey });
const prompt = `
Generate a 5-step actionable learning roadmap for mastering "${goal}".
Rules:
- Return ONLY a valid JSON array
- No markdown, no explanations, no code fences
- Each step must include: id, title, description, duration
- Do NOT include links or resources
Example format:
[
{ "id": 1, "title": "...", "description": "...", "duration": "3 weeks" }
]
`;
const response = await gemini.models.generateContent({
model: "gemini-2.5-flash",
contents: prompt
});
let text = response.text?.trim() || "";
text = text.replace(/^```json/, "")
.replace(/^```/, "")
.replace(/```$/, "")
.trim();
let roadmap;
try {
roadmap = JSON.parse(text);
if (!Array.isArray(roadmap) || roadmap.length === 0) {
roadmap = fallbackRoadmap;
} else {
// ✅ Normalize response
roadmap = roadmap.map((step, idx) => ({
id: step.id || idx + 1,
title: step.title || fallbackRoadmap[idx].title,
description: step.description || fallbackRoadmap[idx].description,
duration: step.duration || fallbackRoadmap[idx].duration
}));
}
} catch {
roadmap = fallbackRoadmap;
}
res.json({ roadmap });
} catch (err) {
console.error("Gemini API error:", err);
res.status(500).json({
roadmap: fallbackRoadmap,
error: "AI generation failed, fallback used"
});
}
});
// Start server
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});