-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
26 lines (22 loc) · 1.12 KB
/
Copy pathnode.js
File metadata and controls
26 lines (22 loc) · 1.12 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
// GET /api/curriculum/node?course=<raw top-level key>&path=seg1|seg2|...
// Returns the immediate children of the node at that path (each still
// collapsed one level further if it has its own children). Called every
// time the user drills into a course/subject/chapter in the UI.
const { data, shallowChildren, resolveNode } = require('../_lib/data');
module.exports = (req, res) => {
if (req.method !== 'GET') {
res.setHeader('Allow', 'GET');
return res.status(405).json({ error: 'Method not allowed' });
}
const { course, path } = req.query;
if (!course || typeof course !== 'string') {
return res.status(400).json({ error: 'course is required' });
}
const courseNode = data[course];
if (!courseNode) return res.status(404).json({ error: 'Course not found' });
const segments = path ? String(path).split('|').filter(Boolean) : [];
const node = resolveNode(courseNode, segments);
if (node === undefined) return res.status(404).json({ error: 'Path not found' });
res.setHeader('Cache-Control', 'public, s-maxage=300, stale-while-revalidate=3600');
return res.status(200).json(shallowChildren(node));
};