feat: 端口改为 1219,install.sh 支持 systemd 后台部署,增加目录非 git 仓库时自动重建
This commit is contained in:
142
server.cjs
142
server.cjs
@@ -1,69 +1,119 @@
|
||||
/**
|
||||
* QZMusic Web 静态文件服务器
|
||||
* 外网访问支持
|
||||
* QZMusic Web 静态文件服务器(生产用)
|
||||
* 监听 0.0.0.0:1219,支持公网访问
|
||||
*/
|
||||
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const url = require('url');
|
||||
|
||||
const PORT = 10096;
|
||||
const PORT = parseInt(process.env.QZMUSIC_PORT || '1219', 10);
|
||||
const HOST = process.env.QZMUSIC_HOST || '0.0.0.0';
|
||||
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',
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.htm': 'text/html; charset=utf-8',
|
||||
'.js': 'application/javascript; charset=utf-8',
|
||||
'.mjs': 'application/javascript; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.json': 'application/json; charset=utf-8',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.jpeg': 'image/jpeg',
|
||||
'.gif': 'image/gif',
|
||||
'.webp': 'image/webp',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.ico': 'image/x-icon',
|
||||
'.ttf': 'font/ttf',
|
||||
'.woff': 'font/woff',
|
||||
'.woff2': 'font/woff2'
|
||||
'.woff2':'font/woff2',
|
||||
'.map': 'application/json; charset=utf-8',
|
||||
'.txt': 'text/plain; charset=utf-8'
|
||||
};
|
||||
|
||||
function log(...args) {
|
||||
const ts = new Date().toISOString().replace('T', ' ').substring(0, 19);
|
||||
console.log(`[${ts}]`, ...args);
|
||||
}
|
||||
|
||||
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');
|
||||
const parsed = url.parse(req.url);
|
||||
let pathname = decodeURIComponent(parsed.pathname || '/');
|
||||
if (pathname === '/') pathname = '/index.html';
|
||||
|
||||
let filePath = path.join(ROOT, pathname);
|
||||
|
||||
// 防止路径穿越
|
||||
if (!filePath.startsWith(ROOT)) {
|
||||
res.writeHead(403);
|
||||
res.end('Forbidden');
|
||||
return;
|
||||
}
|
||||
|
||||
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');
|
||||
const extname = String(path.extname(filePath)).toLowerCase();
|
||||
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
||||
|
||||
// SPA 路由:如果不是带扩展名的文件且文件不存在,则回退到 index.html
|
||||
const tryStatic = (cb) => {
|
||||
fs.stat(filePath, (err, stat) => {
|
||||
if (!err && stat.isDirectory()) {
|
||||
filePath = path.join(filePath, 'index.html');
|
||||
return fs.stat(filePath, (e2, s2) => {
|
||||
if (!e2 && s2.isFile()) return cb(true);
|
||||
return cb(false);
|
||||
});
|
||||
} 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');
|
||||
if (!err && stat.isFile()) return cb(true);
|
||||
return cb(false);
|
||||
});
|
||||
};
|
||||
|
||||
tryStatic((found) => {
|
||||
if (!found) {
|
||||
filePath = path.join(ROOT, 'index.html');
|
||||
}
|
||||
fs.readFile(filePath, (error, content) => {
|
||||
if (error) {
|
||||
log(`${req.method} ${req.url} -> 500 (${error.code})`);
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
res.end('Server Error: ' + error.code);
|
||||
} else {
|
||||
const finalType = found ? contentType : 'text/html; charset=utf-8';
|
||||
res.writeHead(200, {
|
||||
'Content-Type': finalType,
|
||||
'Cache-Control': extname === '.html' || extname === '.htm'
|
||||
? 'no-cache, no-store, must-revalidate'
|
||||
: 'public, max-age=3600'
|
||||
});
|
||||
res.end(content);
|
||||
log(`${req.method} ${req.url} -> 200 (${pathname})`);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`
|
||||
╔═══════════════════════════════════════════════════════════════════════╗
|
||||
║ ║
|
||||
║ QZMusic Web Server 启动成功! ║
|
||||
║ ║
|
||||
║ 本地访问:http://localhost:${PORT} ║
|
||||
║ 局域网访问:http://[你的IP]:${PORT} ║
|
||||
║ ║
|
||||
║ 外网访问:请配置端口转发/公网IP访问 ║
|
||||
║ ║
|
||||
╚═══════════════════════════════════════════════════════════════════════╝
|
||||
`);
|
||||
server.listen(PORT, HOST, () => {
|
||||
const banner = `
|
||||
╔══════════════════════════════════════════════════════════════╗
|
||||
║ ║
|
||||
║ QZMusic Web Server 启动成功! ║
|
||||
║ ║
|
||||
║ 监听地址: http://${HOST}:${PORT} ║
|
||||
║ 本地访问: http://localhost:${PORT} ║
|
||||
║ 公网访问: http://[你的公网IP]:${PORT} ║
|
||||
║ ║
|
||||
╚══════════════════════════════════════════════════════════════╝
|
||||
`;
|
||||
console.log(banner);
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
log('收到 SIGINT,正在关闭服务器...');
|
||||
server.close(() => process.exit(0));
|
||||
});
|
||||
process.on('SIGTERM', () => {
|
||||
log('收到 SIGTERM,正在关闭服务器...');
|
||||
server.close(() => process.exit(0));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user