-
-
Save whatl3y/64a08d117b5856c21599b650c4dd69e6 to your computer and use it in GitHub Desktop.
check if a port is being used with nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net') | |
function isPortTaken(port) { | |
return new Promise((resolve, reject) => { | |
const tester = net.createServer() | |
tester.once('error', err => { | |
if (err.code != 'EADDRINUSE') | |
return reject(err) | |
resolve(true) | |
}) | |
.once('listening', () => tester.once('close', () => resolve(false)).close()) | |
.listen(port) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment