diff options
Diffstat (limited to 'serve.js')
| -rw-r--r-- | serve.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/serve.js b/serve.js new file mode 100644 index 0000000..9e71f46 --- /dev/null +++ b/serve.js @@ -0,0 +1,49 @@ +#!/usr/bin/env node +/** + * Simple static file server for development + */ + +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +const PORT = process.argv[2] || 9000; + +const MIME_TYPES = { + '.html': 'text/html', + '.css': 'text/css', + '.js': 'application/javascript', + '.json': 'application/json', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.png': 'image/png', + '.md': 'text/markdown', + '.ico': 'image/x-icon' +}; + +const server = http.createServer((req, res) => { + let filePath = '.' + (req.url === '/' ? '/index.html' : req.url); + filePath = decodeURIComponent(filePath); + + const ext = path.extname(filePath).toLowerCase(); + const contentType = MIME_TYPES[ext] || 'application/octet-stream'; + + fs.readFile(filePath, (err, content) => { + if (err) { + if (err.code === 'ENOENT') { + res.writeHead(404); + res.end('Not found'); + } else { + res.writeHead(500); + res.end('Server error'); + } + } else { + res.writeHead(200, { 'Content-Type': contentType }); + res.end(content); + } + }); +}); + +server.listen(PORT, () => { + console.log(`Server running at http://localhost:${PORT}/`); +}); |
