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+/);
|
||||
if(parts[0]==='写入'){
|
||||
const filepath=parts[1];
|
||||
const contentIdx=text.indexOf('\n',text.indexOf(t)+t.length)+1;
|
||||
const content=text.slice(contentIdx).replace(/^```[\s\S]*?\n/,'').replace(/\n```$/,'');
|
||||
ops.push({type:'write',path:filepath,content:content});
|
||||
ops.push({type:'write',path:filepath});
|
||||
}else if(parts[0]==='命令'){
|
||||
ops.push({type:'shell',cmd:parts.slice(1).join(' ')});
|
||||
}
|
||||
@ -115,8 +113,7 @@ async function runHLDP(hlpdText){
|
||||
try{
|
||||
const dir=path.dirname(op.path);
|
||||
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,size:Buffer.byteLength(op.content,'utf-8')});
|
||||
results.push({type:'write',path:op.path,ok:true});
|
||||
}catch(e){results.push({type:'write',path:op.path,ok:false,error:e.message});}
|
||||
}else if(op.type==='python'){
|
||||
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});}
|
||||
});
|
||||
|
||||
/* ═══ 启动 + 错误处理 ═══ */
|
||||
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',()=>{
|
||||
const h=os.hostname();
|
||||
log('INFO','startup','host='+h+' port='+PORT+' v2.0');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user