Rayn D Rayn D - 1 year ago 78
Node.js Question

Using BB promise to create server with free port

When I run the create server & listen in my unit test some times I'm getting the following error:


Now I want to handle it by using module portscanner to find free port

I do it like this

var http = require('http');
var Promise = require('bluebird');
var portscanner = require('portscanner');

var server = http.createServer(function (req, res) {
if (req.url == '/fail') {
} else {
res.writeHead(200, {"Content-Type": "text/plain"});



module.exports = server;

This was working but not sure if it's stable code

my question is if it's OK to use it like this? and this is not some dirty workaround and if yes how can I handle it better?

Answer Source

Not sure why you are using promise in this case, you can simply use something like :

portscanner.findAPortNotInUse(3000, 5000, '', (error, port) =>{
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download