How to catch the exception "Port is already used" #3903
-
I want my app to quit whenever raising exception port is already used. How can I handle it? |
Beta Was this translation helpful? Give feedback.
Answered by
darrachequesne
May 3, 2021
Replies: 1 comment
-
You can catch the 'EADDRINUSE' error event: const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer, { /* ... */ });
const port = 8080;
io.on("connection", (socket) => { /* ... */ });
server.listen(port, () => {
console.log(`server listening on port ${port}`)
});
server.on("error", (e) => {
if (e.code === "EADDRINUSE") {
process.exit(1);
}
}); Reference: https://nodejs.org/api/net.html#net_server_listen |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
darrachequesne
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can catch the 'EADDRINUSE' error event:
Reference: https://nodejs.org/api/net.html#net_server_listen