100+ AI Agents, Agent Skills and RAG Apps - Free and Open Source.
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = Number(process.env.PORT) || 3000;
const ROOT = path.resolve(__dirname);
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.excaliflow': 'application/json; charset=utf-8',
'.excalidraw': 'application/json; charset=utf-8',
'.png': 'image/png',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff2': 'font/woff2',
'.woff': 'font/woff',
'.ttf': 'font/ttf',
'.webm': 'video/webm',
'.mp4': 'video/mp4'
};
const server = http.createServer((req, res) => {
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.writeHead(405, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('405 Method Not Allowed');
return;
}
let decodedUrl = '/';
try {
decodedUrl = decodeURIComponent((req.url || '/').split('?')[0]);
} catch {
res.writeHead(400, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('400 Bad Request');
return;
}
const relPath = decodedUrl === '/' ? 'index.html' : decodedUrl.replace(/^\/+/, '');
const safePath = path.resolve(ROOT, relPath);
if (safePath !== ROOT && !safePath.startsWith(ROOT + path.sep)) {
res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('403 Forbidden');
return;
}
fs.stat(safePath, (statErr, stats) => {
if (statErr || !stats.isFile()) {
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('404 Not Found');
return;
}
const ext = path.extname(safePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
if (req.method === 'HEAD') {
res.writeHead(200, {
'Content-Type': contentType,
'Content-Length': stats.size,
'Cache-Control': 'no-cache'
});
res.end();
return;
}
fs.readFile(safePath, (readErr, data) => {
if (readErr) {
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('500 Internal Server Error');
return;
}
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': 'no-cache'
});
res.end(data);
});
});
});
if (require.main === module) {
server.listen(PORT, () => {
console.log(`Excaliflow Studio running at http://localhost:${PORT}`);
});
}
module.exports = server;