feat: 添加外网访问支持
This commit is contained in:
69
server.cjs
Normal file
69
server.cjs
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* 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访问 ║
|
||||
║ ║
|
||||
╚═══════════════════════════════════════════════════════════════════════╝
|
||||
`);
|
||||
});
|
||||
Reference in New Issue
Block a user