Spaces:
Running
Running
| const http = require('http'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const port = process.env.PORT || 5000; | |
| const mimeTypes = { | |
| '.html': 'text/html', | |
| '.js': 'text/javascript', | |
| '.css': 'text/css', | |
| '.json': 'application/json', | |
| '.png': 'image/png', | |
| '.jpg': 'image/jpeg', | |
| '.gif': 'image/gif', | |
| '.svg': 'image/svg+xml', | |
| '.ico': 'image/x-icon' | |
| }; | |
| const server = http.createServer((req, res) => { | |
| let filePath = '.' + req.url; | |
| if (filePath === './') filePath = './index.html'; | |
| const extname = path.extname(filePath).toLowerCase(); | |
| const contentType = mimeTypes[extname] || 'application/octet-stream'; | |
| fs.readFile(filePath, (error, content) => { | |
| if (error) { | |
| if (error.code === 'ENOENT') { | |
| res.writeHead(404); | |
| res.end('File not found'); | |
| } else { | |
| res.writeHead(500); | |
| res.end('Server error'); | |
| } | |
| } else { | |
| res.writeHead(200, { 'Content-Type': contentType }); | |
| res.end(content, 'utf-8'); | |
| } | |
| }); | |
| }); | |
| server.listen(port, '0.0.0.0', () => { | |
| console.log(`Server running at http://localhost:${port}/`); | |
| }); | |