Change port if specific port is already used - node.js

If I try to run the react app in one terminal, it opens on port 8000, and if I open a new terminal tab and try to run the app again I get the following, after which it opens the app on port 8001
Something is already running on port 8000.
Would you like to run the app on another port instead?(Y/n)
And this is possible because of:
"start": "react-scripts start"
Most of the solutions suggest to kill the process that occurs in the port.
But I'm trying to implement this sort of function for a Node(Express) application, such that if a particular port is running/in use, it should just open the app in another port.
I've also thought of this, as a solution, but this would only let me open the app either in 8000 or 8001. I want the app to be able to open on 'n' number of terminals with 'n' different ports. Essentially, is there a NodeJS alternative to "start": "react-scripts start" ?

Using express you can omit the port params when calling app.listen, which you make express run your app on a random unused port.
Doc: https://expressjs.com/en/4x/api.html#app.listen
EDIT:
The solution to the problem can be found here

Related

json-server running but can't access its api due to 403

Learning reactjs, trying to simulate a server with json-server
this is my script to run the server:
"scripts": {
"server": "json-server --watch db.json --port 5000"
},
running this on terminal npm run server
output:
Debugger listening on ws://127.0.0.1:61616/507f7893-92a9-4526-b8f4-a3e71cfa4c62
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
> my-app#0.1.0 server
> json-server --watch db.json --port 5000
Debugger listening on ws://127.0.0.1:61629/8a2f877f-f4bd-465e-b454-01a63eabb40a
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:5000/tasks
Home
http://localhost:5000
Type s + enter at any time to create a snapshot of the database
Watching...
now when I try to access http://localhost:5000/tasks , I get 403 Access to localhost was denied.
what can I do to fix this?
Thanks!
the problem was with the port. changed it to 3001 and it works.
still doesn't understand why did this happen in that port.
Maybe you are using MacOS Monterey(or maybe other versions in the future). Check the reason here: https://developer.apple.com/forums/thread/682332
Cause: Control Center on Monterey listening to port 5000 and port 7000
Fix: change the port to avoid range 5000-7000, like following,
"server": "json-server --watch db.json --port 3000"
or just remove the customised argument --port 3000 as this is the default value
Search for AirPlay receiver and uncheck it if you are a mac user. Worked for me on port 5000.
Reason - This is apparently due to the new AirPlay functionality. Control Center stops listening to those ports when I turn off “AirPlay Receiver” in the “Sharing” System Preference:
Source- https://developer.apple.com/forums/thread/682332

How to run react port and backend port in different ports?

I run a react frontend application. I also serve an API in the backend. When I do "node server.js" it runs at port 5000 and then when I npm start it tries to run at port 5000 but as it is busy, I choose a different one and it goes to 5001.
However, if I use nodemon and concurrently to run both frontend and backend in one command, there is no backlash asking me that the ports are busy. I suspect backend does not run if I do that because I cannot reach it through localhost/api/data link.
How can I run them in different ports? I use craco in start scripts because I use tailwindcss, so cross-env does not work
I suspect that you're using the create-react-app boilerplate.
In that scenario you can add a proxy field in the package.json file to tell the react server to forward requests locally to a separate endpoint.
More on this is available here: https://create-react-app.dev/docs/proxying-api-requests-in-development/
If your backend is running on port 5000 add the following to your package.json
"proxy": "http://localhost:5000"
Once that's done start up the react server and it should do the forwarding on its own irrelevant of cors.
What you can do here is provide a proxy and a PORT:
"proxy": "http://localhost:5000"
...
"scripts": {
"start": "PORT=2000 react-scripts start",
...
This way the server will start on 2000 an all HTTP calls will be proxied to 5000.

Port not running but app wont stop running in browser - Windows - React - Node

So when I go to localhost:3001 it shows project I worked on before. I restart my computer, its still there, kill all node/vscode processes in task manager, its still there. I tried the npx kill port 3001 - seems ok says "port killed" but, the app still there in localhost 3001. When I use netstat findstr 3001 shows nothing there. When I use CurrPort or cmd to check all ports listening - 3001 doesn't show up.
If I listen in port 3000, everything works normal, but when I npm start another app in React itll take me to port 3001 and it doesnt give any error. Except when it opens on Port 3001 it doesnt display the app I npm started in, it still shows that ghost app that I cant get out of port 3001.
In the network tab of the ghost app/port 3001 it shows port 3001 there. I cant post screenshots cus low reputation but idk if something with the initiators in network tab might give a hint to what the problem is.
I could always just set it so React uses 3002 onwards or just run one app at a time in port:3000 buts its bothering that this ghost app has taken over a port.
Hopefully its just something stupid Im overlooking, but if anybody ever had a similar experience let me know!
TL:DR my port 3001 is showing not running listening everywhere I check but everytime I go to it it shows this old project and if I try to run another app at the port terminal says its running, everything ok ,but it is still that old app still there
So it seemed to be something with the browser not port, worked normal in another browser. So went into the inspector tools of the browser went in storage and deleted all cookies, cache, local&session storage and that fixed it. Also killing the service-worker.js process in the port worked for someone else who had the same problem from the project.

WebSockets request was expected error when using --inspect-brk option

When I run nodemon dist/server/app.js it works on default port 3000 and I'm able to reach my API. But if I run
nodemon --inspect-brk=localhost:3000 dist/server/app.js
I got error
WebSockets request was expected
What's wrong?
You can't run your web server and the debugger on the same port. They are each separate servers (the debugger is a server built into the node.js runtime).
So, you can either remove the port and host designation from the --inspect-brk option and just let it use the defaults (which is all I ever use) or you can select a different port for the debugger that doesn't conflict with your web server or anything else running on that host.

Meteor always listens on port 3000

I created a new Meteor project in WebStorm on Windows 8.
In "Run/Debug Configurations" I set the port value to 3008, but when I run the app, it always works on port 3000.
Does anybody know where Meteor defines the port number or how can I change it?
I've searched the words "3000", "PORT" and "listen" in the entire project but they don't exist.
I've also seen these questions which didn't help me because they use Express which defines the port number hard-coded:
Node.js/Express.js App Only Works on Port 3000 ,
Express is listening on port 3000 despite setting it to 80?
Any help will be profoundly appreciated!
Try passing -p 3008 as a program argument in Meteor run configuration:
http://localhost:3008 in Browser/Live Edit tab is just used for browser launching (i.e. it tells WebStorm to launch the browser with specified URL), it's not supposed to affect Meteor port in any way

Resources