Skip to content

Instantly share code, notes, and snippets.

@Flackus
Last active February 3, 2016 18:13
Show Gist options
  • Save Flackus/3c995cd735c44425c391 to your computer and use it in GitHub Desktop.
Save Flackus/3c995cd735c44425c391 to your computer and use it in GitHub Desktop.
Find unused port in node.js
#!/usr/bin/env node
'use strict';
const net = require('net');
const server = net.createServer();
let port;
server.listen(0, () => {
port = server.address().port;
server.once('close', () => {
console.log(`Found unused port: ${port}`);
});
server.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment