refactor: improve server initialization and port handling
- Separate server setup from initialization\n- Add port availability check utility\n- Fix double server start issue\n- Improve error handling for port conflicts
This commit is contained in:
parent
ce97671da3
commit
2ac1cb3943
3 changed files with 63 additions and 0 deletions
18
src/utils/portCheck.ts
Normal file
18
src/utils/portCheck.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import net from 'net';
|
||||
|
||||
export function isPortAvailable(port: number | string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const server = net.createServer();
|
||||
|
||||
server.once('error', () => {
|
||||
resolve(false);
|
||||
});
|
||||
|
||||
server.once('listening', () => {
|
||||
server.close();
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
server.listen(port);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue