-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (99 loc) · 3.05 KB
/
server.js
File metadata and controls
118 lines (99 loc) · 3.05 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
const express = require('express');
const path = require('path');
const { getAllEvents } = require('./lib/events');
const { getAllProjects } = require('./lib/projects');
const app = express();
const PORT = process.env.PORT || 3000;
// Set Pug as templating engine
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Serve project-adjacent assets (images etc.) colocated with their markdown.
// Falls through to route handlers when the requested file doesn't exist.
app.use('/projects', express.static(path.join(__dirname, 'projects'), {
extensions: false,
index: false,
redirect: false,
}));
// Routes
app.get('/', (req, res) => {
res.render('index', { activePage: 'Home' });
});
app.get('/events', (req, res) => {
res.render('events', { activePage: 'Events' });
});
app.get('/contact', (req, res) => {
res.render('contact', { activePage: 'Visiting / Contact' });
});
app.get('/presskit', (req, res) => {
res.render('presskit', { activePage: 'Press Kit' });
});
app.get('/projects', (req, res) => {
res.render('projects', { activePage: 'Projects' });
});
app.get('/event-list', (req, res) => {
res.render('event-list');
});
// Individual event page
app.get('/events/:eventId(*)', async (req, res) => {
try {
const rawId = req.params.eventId;
const events = await getAllEvents();
const event = events.find(e => e.uid === `md-${rawId}` || e.uid === rawId);
if (!event) {
return res.redirect('/events');
}
res.render('event-detail', {
activePage: 'Events',
event: event,
eventPath: req.params.eventId
});
} catch (error) {
console.error('Error loading event:', error);
res.redirect('/events');
}
});
// API endpoint for events
app.get('/api/events.json', async (req, res) => {
try {
const events = await getAllEvents();
res.json(events);
} catch (error) {
console.error('Error reading markdown events:', error);
res.status(500).json({ error: 'Failed to load markdown events' });
}
});
// Individual project page
app.get('/projects/:projectId(*)', async (req, res) => {
try {
const rawId = req.params.projectId;
const projects = await getAllProjects();
const project = projects.find(p => p.uid === `md-${rawId}` || p.uid === rawId);
if (!project) {
return res.redirect('/projects');
}
res.render('project-detail', {
activePage: 'Projects',
project: project,
projectPath: req.params.projectId
});
} catch (error) {
console.error('Error loading project:', error);
res.redirect('/projects');
}
});
// API endpoint for projects
app.get('/api/projects.json', async (req, res) => {
try {
const projects = await getAllProjects();
res.json(projects);
} catch (error) {
console.error('Error reading markdown projects:', error);
res.status(500).json({ error: 'Failed to load markdown projects' });
}
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});