Spaces:
Running on Zero
Running on Zero
File size: 1,479 Bytes
ba70497 | 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 | // Zero-dependency static server. Run: node server.js -> http://localhost:3000
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 3000;
const ROOT = path.join(__dirname, 'public');
const GRAPH = path.join(__dirname, 'data', 'graph.json');
const TYPES = {
'.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8', '.json': 'application/json',
'.svg': 'image/svg+xml', '.png': 'image/png',
};
http.createServer((req, res) => {
const url = decodeURIComponent(req.url.split('?')[0]);
// Graph data is read fresh on every request, so edits to data/graph.json show on refresh.
if (url === '/api/graph') {
return fs.readFile(GRAPH, (err, buf) => {
if (err) { res.writeHead(500); return res.end('Could not read data/graph.json'); }
res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
res.end(buf);
});
}
const file = path.normalize(path.join(ROOT, url === '/' ? 'index.html' : url));
if (!file.startsWith(ROOT)) { res.writeHead(403); return res.end('Forbidden'); }
fs.readFile(file, (err, buf) => {
if (err) { res.writeHead(404); return res.end('Not found'); }
res.writeHead(200, { 'Content-Type': TYPES[path.extname(file)] || 'application/octet-stream' });
res.end(buf);
});
}).listen(PORT, () => console.log(`Orb graph running at http://localhost:${PORT}`));
|