-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (29 loc) · 732 Bytes
/
index.js
File metadata and controls
33 lines (29 loc) · 732 Bytes
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
28
29
30
31
32
33
// index.js
require("dotenv").config();
const app = require("./src/app");
const { port } = require("./config/app");
const startServer = (port) => {
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
};
const findAvailablePort = (port) => {
const server = require("http").createServer();
// start server
server.listen(port, () => {
server.close(() => {
startServer(port);
});
});
// Listen for server errors
server.on("error", (err) => {
if (err.code == "EADDRINUSE") {
console.error(
`ERROR: Port ${port} is already in use. Trying the next one...`
);
findAvailablePort(+port + 1);
}
});
};
// Entry Point
findAvailablePort(port);