diff options
author | Calvin Morrison <calvin@pobox.com> | 2025-09-03 21:15:36 -0400 |
---|---|---|
committer | Calvin Morrison <calvin@pobox.com> | 2025-09-03 21:15:36 -0400 |
commit | 49fa5aa2a127bdf8924d02bf77e5086b39c7a447 (patch) | |
tree | 61d86a7705dacc9fddccc29fa79d075d83ab8059 /client/server.js |
Diffstat (limited to 'client/server.js')
-rw-r--r-- | client/server.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/client/server.js b/client/server.js new file mode 100644 index 0000000..56a8995 --- /dev/null +++ b/client/server.js @@ -0,0 +1,59 @@ +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const url = require('url'); + +const port = process.env.PORT || 3000; + +const mimeTypes = { + '.html': 'text/html', + '.js': 'text/javascript', + '.css': 'text/css', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpg', + '.gif': 'image/gif', + '.svg': 'image/svg+xml', + '.ico': 'image/x-icon' +}; + +const server = http.createServer((req, res) => { + console.log(`${req.method} ${req.url}`); + + let filePath = '.' + url.parse(req.url).pathname; + + // Default to index.html + if (filePath === './') { + filePath = './index.html'; + } + + const extname = String(path.extname(filePath)).toLowerCase(); + const mimeType = mimeTypes[extname] || 'application/octet-stream'; + + fs.readFile(filePath, (error, content) => { + if (error) { + if (error.code === 'ENOENT') { + // File not found + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('404 Not Found\n'); + } else { + // Server error + res.writeHead(500, { 'Content-Type': 'text/plain' }); + res.end('500 Internal Server Error\n'); + } + } else { + // Success + res.writeHead(200, { + 'Content-Type': mimeType, + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, Authorization' + }); + res.end(content, 'utf-8'); + } + }); +}); + +server.listen(port, () => { + console.log(`JCHAT Client Server running at http://localhost:${port}/`); +}); |