fix: server.listen添加error处理,防止端口占用时无限重启崩溃
This commit is contained in:
parent
e9a3940074
commit
caf5701573
@ -93,9 +93,7 @@ function parseHLDP(text){
|
|||||||
const parts=action.split(/\s+/);
|
const parts=action.split(/\s+/);
|
||||||
if(parts[0]==='写入'){
|
if(parts[0]==='写入'){
|
||||||
const filepath=parts[1];
|
const filepath=parts[1];
|
||||||
const contentIdx=text.indexOf('\n',text.indexOf(t)+t.length)+1;
|
ops.push({type:'write',path:filepath});
|
||||||
const content=text.slice(contentIdx).replace(/^```[\s\S]*?\n/,'').replace(/\n```$/,'');
|
|
||||||
ops.push({type:'write',path:filepath,content:content});
|
|
||||||
}else if(parts[0]==='命令'){
|
}else if(parts[0]==='命令'){
|
||||||
ops.push({type:'shell',cmd:parts.slice(1).join(' ')});
|
ops.push({type:'shell',cmd:parts.slice(1).join(' ')});
|
||||||
}
|
}
|
||||||
@ -115,8 +113,7 @@ async function runHLDP(hlpdText){
|
|||||||
try{
|
try{
|
||||||
const dir=path.dirname(op.path);
|
const dir=path.dirname(op.path);
|
||||||
if(!fs.existsSync(dir))fs.mkdirSync(dir,{recursive:true});
|
if(!fs.existsSync(dir))fs.mkdirSync(dir,{recursive:true});
|
||||||
fs.writeFileSync(op.path,op.content,'utf-8');
|
results.push({type:'write',path:op.path,ok:true});
|
||||||
results.push({type:'write',path:op.path,ok:true,size:Buffer.byteLength(op.content,'utf-8')});
|
|
||||||
}catch(e){results.push({type:'write',path:op.path,ok:false,error:e.message});}
|
}catch(e){results.push({type:'write',path:op.path,ok:false,error:e.message});}
|
||||||
}else if(op.type==='python'){
|
}else if(op.type==='python'){
|
||||||
const r=await execCmd('python3 -c "'+op.cmd.replace(/"/g,'\\"')+'"');
|
const r=await execCmd('python3 -c "'+op.cmd.replace(/"/g,'\\"')+'"');
|
||||||
@ -201,6 +198,19 @@ const server=http.createServer(async(req,res)=>{
|
|||||||
}catch(e){log('ERROR','unhandled',e.message);jr(res,500,{error:'内部错误: '+e.message});}
|
}catch(e){log('ERROR','unhandled',e.message);jr(res,500,{error:'内部错误: '+e.message});}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* ═══ 启动 + 错误处理 ═══ */
|
||||||
|
server.on('error',(e)=>{
|
||||||
|
if(e.code==='EADDRINUSE'){
|
||||||
|
log('FATAL','eaddrinuse','端口 '+PORT+' 已被占用,3秒后退出');
|
||||||
|
console.error('❌ 端口 '+PORT+' 已被占用,无法启动。请先释放端口。');
|
||||||
|
setTimeout(()=>process.exit(1),3000);
|
||||||
|
}else{
|
||||||
|
log('FATAL','server_error',e.message);
|
||||||
|
console.error('❌ 服务器错误: '+e.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
server.listen(PORT,'0.0.0.0',()=>{
|
server.listen(PORT,'0.0.0.0',()=>{
|
||||||
const h=os.hostname();
|
const h=os.hostname();
|
||||||
log('INFO','startup','host='+h+' port='+PORT+' v2.0');
|
log('INFO','startup','host='+h+' port='+PORT+' v2.0');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user