27 lines
942 B
JavaScript
27 lines
942 B
JavaScript
|
|
const http = require('http');
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const PORT = 3444;
|
||
|
|
const ROOT = path.join(__dirname, 'out');
|
||
|
|
const MIME = { '.html':'text/html','.js':'text/javascript','.css':'text/css','.png':'image/png','.svg':'image/svg+xml','.ico':'image/x-icon','.json':'application/json' };
|
||
|
|
|
||
|
|
http.createServer((req, res) => {
|
||
|
|
let fp = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
|
||
|
|
// SPA fallback: serve index.html for non-file routes
|
||
|
|
if (!fs.existsSync(fp) || fs.statSync(fp).isDirectory()) {
|
||
|
|
fp = path.join(ROOT, 'index.html');
|
||
|
|
}
|
||
|
|
if (fs.existsSync(fp)) {
|
||
|
|
const ext = path.extname(fp);
|
||
|
|
res.writeHead(200, { 'Content-Type': MIME[ext] || 'text/html' });
|
||
|
|
fs.createReadStream(fp).pipe(res);
|
||
|
|
} else {
|
||
|
|
res.writeHead(404);
|
||
|
|
res.end('Not found');
|
||
|
|
}
|
||
|
|
}).listen(PORT, () => {
|
||
|
|
console.log(IPFS Portal: http://localhost:);
|
||
|
|
console.log(Network: http://100.112.2.113:);
|
||
|
|
});
|