bykof / cordova-plugin-webserver

A webserver plugin for cordova
Other
119 stars 51 forks source link

port issue #31

Open ajayashuba opened 5 years ago

ajayashuba commented 5 years ago

how do i make sure the port is not used by any other instance if i choose port by myself there is some % chance that that port might be in use ..is there way to programatically access the free port given by the system

dev-ankur commented 4 years ago

@ajayashuba Its been months, have you got any solution? I really need to know! I'm using ionic 4

oddmario commented 11 months ago

For all the recent and future readers, you can use https://github.com/doppiaQ/cordova-plugin-portfinder

Then start the webserver like that:

portfinder.getPort().then((port) => {
    webserver.start(
          function() {
            console.log("Started the HTTP server on port ", port);
          },
          function() {
            console.log('Failed to start the HTTP server');
          },
          port
    )
}).catch((e) => {
    console.log("Failed to find a free port");
});