gbowne1 / RadioLogger

A Radio Logging application build with NodeJS and ExpressJS
GNU General Public License v3.0
6 stars 7 forks source link

[BUG] Server will not forward port if 3000 is already in use #73

Closed gbowne1 closed 1 year ago

gbowne1 commented 1 year ago

The backend and server will not forward the port if 3000 is already in use by something else.

You might see something like this in the server from nodemon. It should ask you if you want to choose the next available port like the Create React App development server does.

[2023-07-17 14:10:41:1041]:[error] uncaughtException: listen EADDRINUSE: address already in use :::3000
Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1868:16)
    at listenInCluster (node:net:1916:12)
    at Server.listen (node:net:2004:7)
    at Function.listen (/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/RadioLogger/src/server/node_modules/express/lib/application.js:635:24)
    at Object.<anonymous> (/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/RadioLogger/src/server/server.js:62:20)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
jzunigarce commented 1 year ago

In .env of server folder i going to add a variable for set a port for the backend app and you van put any port

gbowne1 commented 1 year ago

It should not be any specific port. I forget how CRA does it, but it should see that 3000 is in use then ask you if you want to choose the next available port, in this case 3001 and then you just push y in the terminal/console to send the server to 3001 and it opens in 3001.

gbowne1 commented 1 year ago

could write a server util to do this.

gbowne1 commented 1 year ago

If I am working in many projects I have to start RadioLogger first then the other react, etc. projects that take 3xxx ports next.

gbowne1 commented 1 year ago

I don't know if we are doing process.ENV.production and development. "production" would not have this functionality as it would only run on one port. "dev" could run on any port in the 3000-3999 range (as node &&& express lives in this range.)

jzunigarce commented 1 year ago

Would it be useful if each one of us puts the port they want in the .env? that way the port I set doesn't need to be the same one you use. Another alternative is to use docker and set a port that is idle and maps to 3000

gbowne1 commented 1 year ago

yes, that could be useful, just set a port. I typically have something already running on 3000 besides RadioLogger. I think we worked on this issue, but its not totally working in an ideal manner. I am also thinking of those other users and people that have something else running on 3000 already. I've had to go in and change the port locally at times.

Side note, I want to make a Docker container and a .devcontainer (a VSCode thing) for this project eventually. There is an issue for a .devcontainer.

gbowne1 commented 1 year ago

I was thinking we could change line 61 - 64 in the server.js to

const server = app.listen(process.env.PORT || 3000, () => {
  logger.info(`Server is listening on port: ${server.address().port}`);
});

PORT || being the decider. This would make this process easier. || would make the script take either the PORT variable or the 3000..

jzunigarce commented 1 year ago

I made a change, to Night i push

gbowne1 commented 1 year ago

ok. oh and I discovered that there are also some npm modules available for doing the same thing but that would be a lot of work. I also discovered node has a https module built in too.

gbowne1 commented 1 year ago

Closing as fixed with refactor PR.