Rayn D Rayn D - 9 months ago 45
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?


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

portscanner.findAPortNotInUse(3000, 5000, '', (error, port) =>{