| 12345678910111213 |
- const http = require('http');
- const fs = require('fs');
- const path = require('path');
- const PORT = process.env.FRONTEND_PORT || 8081;
- http.createServer((req, res) => {
- let filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);
- const ext = path.extname(filePath);
- const types = { '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript' };
- fs.readFile(filePath, (err, data) => {
- res.writeHead(err ? 404 : 200, { 'Content-Type': types[ext] || 'text/plain' });
- res.end(err ? 'Not Found' : data);
- });
- }).listen(PORT, () => console.log('前端已启动:http://localhost:' + PORT));
|