Files
QZMusic-Web/server.cjs
2026-06-04 15:15:17 +00:00

70 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* QZMusic Web 静态文件服务器
* 外网访问支持
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 10096;
const ROOT = path.join(__dirname, 'dist');
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',
'.ttf': 'font/ttf',
'.woff': 'font/woff',
'.woff2': 'font/woff2'
};
const server = http.createServer((req, res) => {
let filePath = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
if (!extname && !fs.existsSync(filePath)) {
filePath = path.join(ROOT, 'index.html');
}
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
fs.readFile(path.join(ROOT, 'index.html'), (err, html) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html, 'utf-8');
});
} else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`
╔═══════════════════════════════════════════════════════════════════════╗
║ ║
║ QZMusic Web Server 启动成功! ║
║ ║
║ 本地访问http://localhost:${PORT}
║ 局域网访问http://[你的IP]:${PORT}
║ ║
║ 外网访问:请配置端口转发/公网IP访问 ║
║ ║
╚═══════════════════════════════════════════════════════════════════════╝
`);
});