-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
27 lines (23 loc) · 845 Bytes
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { logger } from '@adamgen/logger';
import { Express } from 'express';
import killPort from 'kill-port';
export function forceListen(app: Express) {
const port = parseInt(process.env.PORT) || 3000;
const server = app.listen(port);
server.addListener('error', function listenErrorCallback(err: { code: string }) {
logger.green('killed port', port);
if (err.code === 'EADDRINUSE') {
killPort(port)
.then(() => {
server.listen(port);
})
.catch((killPortErr: Error) => {
console.log(killPortErr);
});
}
});
server.addListener('listening', function listeningCallback() {
logger.green('server code runs on port', port);
logger.message('RESTART_SUCCESS');
});
}