Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (84 loc) · 2.82 KB
/
Copy pathserver.js
File metadata and controls
95 lines (84 loc) · 2.82 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
const express = require('express');
const path = require('path');
const cors = require('cors');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3001;
// Enable CORS for all routes
app.use(cors());
// Serve static files from public directory
app.use(express.static(path.join(__dirname, 'public')));
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Component access without .json suffix (e.g., /r/blog-layout)
app.get('/r/:componentName', (req, res, next) => {
const componentName = req.params.componentName;
// Skip if it already has .json extension
if (componentName.endsWith('.json')) {
return next();
}
const jsonFilePath = path.join(__dirname, 'public', 'r', `${componentName}.json`);
// Check if the JSON file exists
if (fs.existsSync(jsonFilePath)) {
// Set proper content-type and serve the JSON file
res.setHeader('Content-Type', 'application/json');
res.sendFile(jsonFilePath);
} else {
res.status(404).json({
error: 'Component not found',
component: componentName,
available: '/registry'
});
}
});
// Registry listing endpoint - update URLs to remove .json
app.get('/registry', (req, res) => {
res.json({
name: 'unilab-layouts',
description: 'Layout components registry for URPC projects',
components: [
{
name: 'card-layout',
url: `${req.protocol}://${req.get('host')}/r/card-layout`,
description: 'Modern card layout inspired by daily.dev'
},
{
name: 'blog-layout',
url: `${req.protocol}://${req.get('host')}/r/blog-layout`,
description: 'Clean blog-style layout'
},
{
name: 'social-layout',
url: `${req.protocol}://${req.get('host')}/r/social-layout`,
description: 'Social media style feed layout'
},
{
name: 'magazine-layout',
url: `${req.protocol}://${req.get('host')}/r/magazine-layout`,
description: 'Magazine-style article layout'
},
{
name: 'minimal-layout',
url: `${req.protocol}://${req.get('host')}/r/minimal-layout`,
description: 'Minimalist list layout'
},
{
name: 'common-ui',
url: `${req.protocol}://${req.get('host')}/r/common-ui`,
description: 'Common UI components'
}
]
});
});
// Redirect root to registry listing
app.get('/', (req, res) => {
res.redirect('/registry');
});
app.listen(PORT, () => {
console.log(`🚀 Layout Registry Server running at http://localhost:${PORT}`);
console.log(`📦 Registry API: http://localhost:${PORT}/registry`);
console.log(`🔗 Component URLs: http://localhost:${PORT}/r/[component-name]`);
console.log(`💡 Both /r/component-name and /r/component-name.json are supported`);
});