Turn off Node.js Server Once Running on Localhost - node.js

This is probably a simple question but I can't find a clear answer anywhere. I am trying Hello World on node.js. I have a node.js server running on port 8000 of the localhost, turned on via the command line e.g. "node helloworld.js". Helloworld.js runs fine via localhost:8000. Now when I try turn on another server on port 8000 though I get the error "listen EADDRINUSE" because the first server is still running. So how do I turn off the first node server?

Just kill the process by doing ctrl-c...

If you still have the original terminal in which you run the Nodejs server, then simply press ctrl + C can kill the process.
However, if you lost the terminal, then you can open another terminal and run taskkill /F /IM node.exe. (/F to force the kill, /IM to specify which script you want to kill). Note that the command would kill every node server running.

If you no longer have access to your terminal, then go to your task manager on Windows or 'Force Quit' on Mac and end the 'Node.Js...' process.
This is the cleanest way to do it (if no terminal window), in my opinion.

Related

How to stop Yarn Package Manager script from CLI

https://yarnpkg.com/en/docs/cli/
Is there a way to stop what is started from the command "yarn run"? Is my only option to lookup the process number and call kill on it?
The usual way ctrl-c should work. If it doesn't work, than you have bug in the script. The script's author missed handler for shutdown (SIGINT/SIGTERM/etc).
I had a similar issue having it running after ctl+c and then I thought, maybe it is just running on the cache
so went to http://localhost:3000/
ctrl+F5
which forces refresh without cache showed me that the actual project wasn't really running anymore!
;)
*hadn't it worked I would have had to sudo kill the 3000 port
I know this is a well-answered question. However, it behaved once very strange when I was running a sample React code which was auto-created by the create-react-app CLI, on my Windows 10.
After hitting Ctrl+C, which is the most suggested standard way to stop the yarn run, though I got back the command prompt, there was a ghost process lingering around there, which was still actively listening to 3000(default) port, and localhost:3000 was working as normal.
So finally this is how I fixed it:
netstat -ano | grep ":3000" (yeah, I ran this from my git-bash instead of command prompt!)
Noted down the PID of the line where it says LISTENING on 3000
Pressed Ctrl+Shift+Esc to open the Task Manager
Went to the Process tab
Right clicked on one of the headings, say Name
Selected PID --> This added the PID column to the display
Located the PID in question
Right clicked on it and clicked "End task"
Luckily Windows knew how to kill that misbehaving, ghost process and the port became free for me.
NOTE: Prior to the above-mentioned steps, I tried to kill that PID from git-bash using the famous (or notorious as per its meaning?? >8)) kill -9 command. It was responding back with no such PID msg, however netstat -ano was clearly displaying the PID and browser was proving that the ghost process is up and alive!!

getting an error- listen EADDRINUSE :::3000 on windows machine

I've searched a lot and I understand there is some process or server running on port 3000, but how can I stop that and run a new express http server on port 3000. There are few answers which tell it for Unix environment..but how to release the port on windows environment using cmd terminal.
Closing the task using PID also didn't work for me.
Thanks in advance
Open the command prompt and type the following
netstat -a -o -n
run down the list by port until you find port 3000 and you will see the process id. Then run
taskkill /F /PID (yourprocessID)
there's another simple way to do this in a single command
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :3000') DO TaskKill.exe /PID %%P
if you use windows 7 U might need tokens=5, use with caution tokens are different for different OS.
In windows 10, open task manager and go to Details tab.
There you will find node.exe running.
End that task and your port will be released.
If you're using VS Code in Windows, You would need to first KILL all the previous instances of your Node-App
Afterwards, Open the BASH terminal in VS Code and run below command
cmd "/C TASKKILL /IM node.exe /F"
EADDRINUSE means there is already another running process that is listening to the port your node app wants to use and has it reserved. It's most likely you've run the app previously but it hasn't terminated properly and is still holding on to the port.
so first we terminate already running one,then only start another new one server
That's because port 3000 is already busy listening to another process.
so just go to task manager and end that process/ delete all node js related processes.

DigitalOcean stop Node.js server running with nohup

I am running a Node.js server on a DigitalOcean droplet (with Ubuntu). I have worked out how to make it run when I'm not connected to it via Putty. However, just one issue: how do I stop it now?
I can see that control+C works when in the session, but what if I exit the session and come back? How will I stop the server then?
Also, will running it multiple times run multiple servers at once?
Thanks!
You really should be using a tool like supervisord (http://supervisord.org/) for your long-running processes.
But if you want to stop an already running process that you started with nohup then look up the process ID first (with ps aux and look/grep for your process) and then run kill <<pid>>.

LocalHost and MongoDB

I am trying to run MongoDB using a Node express application.
It runs fine for the first time..but once i close and start the code again, I get an error saying that localhost 3000 is already in use.
Also, connecting mongoDB gives an error stating that localhost port 27017 does not accept request
When you run the command npm start it will run in background even after you close it using Ctrl + C. The process running in the background will block the port 3000, since it's being used.
Instead try running with the command node app.js. Change app.js for the main file of your server. (Restart the machine or kill the process before, so you can terminate the process created by your previous command npm start )
Another way to solve that problem is to manually kill the process started with the npm start command. npm start usually is used when the server is in production, since the command makes it run "forever".
Let me know if this solves your problem. :)

Linux TCP/IP Socket Programming

I have developed Linux socket server connection which is working fine.
When start from terminal it start from listening from client. but when I close terminal it stops listening.
I need to continue even though the terminal closed by user from started.
how can I run server socket application in Linux as background process?
am run using ./a.out 8888(portno)
again i will connect error will come (connection refused)
now what do i?
Thank You.
On terminal execute the script ending with &.
A task can usually be started and run as a background task by putting a '&' at the end of the command line.
Check this:
Linux Background Job
You can use nohup to keep the program running if the Terminal is closed:
nohup ./a.out 8888 &
(Standard output/error will be written to nohup.out instead of the Terminal.)

Resources