45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 3000;
|
|
|
|
const MIME_TYPES = {
|
|
'.html': 'text/html',
|
|
'.css': 'text/css',
|
|
'.js': 'text/javascript',
|
|
'.json': 'application/json',
|
|
'.csv': 'text/csv',
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
// Handle URL
|
|
let filePath = '.' + req.url;
|
|
if (filePath === './') {
|
|
filePath = './index.html';
|
|
}
|
|
|
|
const extname = path.extname(filePath);
|
|
const contentType = MIME_TYPES[extname] || 'application/octet-stream';
|
|
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) {
|
|
if (err.code === 'ENOENT') {
|
|
res.writeHead(404);
|
|
res.end('File not found');
|
|
} else {
|
|
res.writeHead(500);
|
|
res.end('Server error: ' + err.code);
|
|
}
|
|
return;
|
|
}
|
|
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
res.end(data);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`Server running at http://localhost:${PORT}/`);
|
|
console.log(`Visualizing spending data with ECharts...`);
|
|
});
|