Last active
November 12, 2016 14:49
-
-
Save herber/43c861cfcf02430c204362a4c61767a9 to your computer and use it in GitHub Desktop.
a simple node js container platform
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 execFile = require('child_process').execFile; | |
var portSet = function(port, fn) { | |
var net = require('net') | |
var tester = net.createServer() | |
.once('error', function (err) { if (err.code != 'EADDRINUSE') return fn(err); fn(null, true) }) | |
.once('listening', function() { | |
tester.once('close', function() { fn(null, false) }) | |
.close() | |
}) | |
.listen(port) | |
} | |
const randPort = Math.floor(Math.random() * (60000 - 10 + 1)) + 10; | |
//change to your file | |
const file = __dirname + '/file.js' | |
portSet(randPort, function (err, yn) { | |
if (!yn) { | |
console.log(randPort); | |
const child = execFile('node', [ file ], { env: {port: randPort} },(error, stdout, stderr) => { | |
if (error) { | |
return; | |
} | |
console.log(stdout); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment