Skip to content

Instantly share code, notes, and snippets.

@whatl3y
Forked from timoxley/isPortTaken.js
Last active December 5, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatl3y/64a08d117b5856c21599b650c4dd69e6 to your computer and use it in GitHub Desktop.
Save whatl3y/64a08d117b5856c21599b650c4dd69e6 to your computer and use it in GitHub Desktop.
check if a port is being used with nodejs
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