How to keep my node js server alive - javascript

I have created an app in Node js including with express js and I will execute the app using the command prompt
c:/myApp > npm start
And it is running my app, but whenever I closed the prompt the server is closing.I have tried with forever like
> npm install forever -g
> forever start --minUptime 1000 --spinSleepTime 1000 app.js
but it is not working either.Is there any way to keep the connection alive.Thanks in advance.

I've used forever for about 3 years. It sucks. It has a ton of bugs and BS that hasn't been fixed after a long time.
I've switched to pm2. It has a much better CLI and API, and a lot less bugs.
CLI:
pm2 start app.js
API:
var pm2 = require('pm2')
pm2.connect(function(err) {
pm2.start({
script: 'runningTest.js'
}, function(err, apps) {
if(err) console.log(err)
pm2.disconnect()
})
})

I use this instruction (Nginx + supervisord) for my projects in production:
http://cuppster.com/2011/05/12/diy-node-js-server-on-amazon-ec2/
Start read from chapter "Have Your Node Server Run Forever"

Related

Node js hangs up on port 3000 I have to restart PC to unblock it

So for past few weeks I have an enormous problem with node on our windows server 2006.
Basically since I installed an update for nodemon using
npm install -g nodemon
Our node started to hang up on our port (default 3000) and the process hangs on Windows. I've tried every possible solution from the web to kill that process and I simply can't -- the only way to unblock the port is to restart whole server.
The port hangs mostly when I want to restart node to implement the changes or when I want to turn it off.
It's not always happening -- mostly it happens after it's been running for 30+ minutes.
My node version is 6.10.0.
Before I updated nodemon something like this never happened before.
And yes, I uninstalled nodemon completely using
npm uninstall -g nodemon
Can anyone guide me to a solution?

Integrate NodeJS MySql with React Application

I have a normal React App which runs on Node Server
-node_modules
-src
-Actions
-Components
-Stores
-Server
-server.js
-package.json
Basically when i run npm start React App will run and for suppose i will be able to see the example in localhost:8080
Now my server.js file includes MySql code
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I have to explicitly run node server.js command to get the connection to mysql and run the queries their.
How do i integrate server.js command in my React App, so that when i run npm start, my MySql connection and the file should execute
The short answer is: you don't.
The short fix is: use some kind of program to help you start both (like npm)
The long answer is:
Set up your npm start task to run both of them. You can do this with concurrently. (npm install --save concurrently)
Change your npm start task (in your package.json) to something like this:
"start": "concurrently -k -r -s first \"node server.js\" \"webpack-dev-server""
If you need further assistance please share your package.json file.
The longest answer is:
This part is OPTIONAL, only follow it if you want to LEARN.
If you want more details you could look at a somewhat more advanced project such as my react-sane-starter to get an idea how to efficiently start multiple services. This project also contains Docker if you're interested.
concurrently over &
People often suggest to run one of the tasks in the background using &, this will usually prevent logs being shown (unless redirected), I'd highly recommend using concurrently to solve this issue. (it will prefix your different services quite nicely)

keep getting "? Something is already running on port 3000" when I do npm start on react app

I keep on getting "? Something is already running on port 3000" message in my terminal when I start up my react server when there is absolutely nothing running on my port 3000
What I have tried to solve with:
Restart the macOS.
checking my "http://localhost:3000" on chrome browser. (Nothing: This site can’t be reached)
Go to chrome://serviceworker-internals and chrome://appcache-internals, search for localhost:3000 (Nothing found)
I also tried almost every command lines I found on Google regarding this issue
I also created another express.js app, and it was successfully launch on port 3000 while React said there is something running on 3000. React on my computer just keeping thinking there is something running on port 3000.
If you are a windows user you can try using
npx kill-port 3000
in your console. I was having the same problem and it worked for me.
Not sure about MAC.
Open cmd and write this
netstat -a -o -n
You will get list of active connections then find 3000 by hitting
Cntrl + f
Copy the PID of that port and hit this command
taskkill /F /PID PID_of_port
Edit
This guide is for windows.
Kill Node.Js process from Task Manager.
Step 1: Open Task Manager by clicking ctrl+shift+delete
Step 2: Open Prcesses tab
Step 3: Search for Node.JS process and right click on that then click on End Task
Step 4: Now you can start again.
As #khurram khan suggested terminating the process may be the best option for you, This work for me on linux:
$ lsof -i tcp:3000
$ kill -9 PID
the first command should give you the PID number to enter in the second command as PID.
I had this problem on Mac and I solved by running:
npx kill-port 3000
I had stuck with this one for few hours, and the end I had found the solution
There was incorrect mapping to the local host in the hosts file and didn't had any relation to the port taken something like
10.2.224.130 localhost
Just Change it back to
127.0.0.1 localhost
Host file locations
https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
In case anyone comes across this and the above solutions didn't help, make sure your /etc/hosts has:
127.0.0.1 localhost
For some reason this was wiped from my hosts file and CRA's dependency for checking ports (detect-port-alt) checks localhost:[PORT] to see if it's available. If it errors out, you'll always get the "Something is already running..." error when checking a specific port but not when using a random port (since that's picked by the dependency).
kill node.js from the background processes. that would soleve the probelem. alterntively, you could restart your system
for window use this in package.json
"start": "set PORT=3006 && react-scripts start"
for Linux and MacOS
"start": "PORT=3006 react-scripts start"
This worked for me on windows pc. This one is for those are not seeing the port when you run this command netstat -a -o -n on your command prompt.
Open your command prompt in administrator mode and run this command
net stop winnat
you'll get this response:
The Windows NAT Driver service was stopped successfully.
Them you run this next:
net start winnat
then you will get this response:
The Windows NAT Driver service was started successfully.
once you do that. Start the react server and it would work. Same too if your backend server doesn't run on 3000
It's very simple. You can fix it in 2 easy steps.
Check your environment variables if there is a key/entry with name "PORT".
If found delete that entry or rename it to something else.
It turns out that some other program is using that variable. Usually when you start react-scripts it will look for an environment variable with that title PORT.
Date: Sat 07, October 2020
Windows: Microsoft Windows 10 Pro Build 19041
Node: 12.16.1
NPM: 6.14.8
Something similar was happening to me on my Windows machine. Tried a lot of things suggested here on StackOverflow and other places.
In my case, I was following a video course that was suggesting adding --script-version 1.1.5 (as illustrated below) when creating a new React app.
create-react-app app-name --scripts-version 1.1.5
Here's the package.json scripts versions:
React: 17.0.1
React DOM: 17.0.1
React Scripts: 1.1.5
After running the command npm start and typing y to try to use a different port, the terminal will stay stucked until it was canceled.
Then I created a new React app without --scripts-version 1.1.5 and after trying to running it, it was still running into Something is already running on port 3000 but after typing y, the app will run with no problem on a different port.
Here's the package.json scripts versions (when it was running successfully):
React: 17.0.1
React-Dom: 17.0.1
React-Scripts: 4.0.0
Starting with the reason you do not see anything on localhost:3000, is because there must be a proper webapp or a website/server running on that port, but the port:3000 is currently running some process, just not of a server/site/app.
This out of the way, depending on your type of OS, it is quite easy to check if there is in-fact a process on port:3000 or not. For MacOS, opening a terminal and running sudo lsof -i ':3000' will list the current process on port 3000.
Note that we need to run command as sudo or root
Once you see what process is running, note the PID and run kill -9 {PID}, which should kill the process with PID (i.e distruptive process on port:3000). Now doing a npm start should get your app built, compiled and served on localhost:3000
Mostly what might've happened is that you had a npm start running which you either interrupted, or closed in the foreground. Doing a fg in your terminal will open any background tasks, so if this opens a npm start instance, it was this process stopping your flow. You can then properly end the session making sure all processes on port:3000 are killed.
Happy coding
If you are on linux you can try
pkill -f node
to terminate the processs
The error occurs when a task is left running on the port properly killing it.
this error can occur on both ports. Your react app and your node server.
To solve this you can run the following commands.
netstat -anp tcp | grep 3000
This command in the terminal will give you the list of activities on port 3000
npx kill-port 3000
This command will kill all the running servers on port 3000.
Now if you have the same problem for your Node server. You can follow the same steps.
netstat -anp tcp | grep 5000
Then
npx kill-port 5000
You don't need the first command. it's just to list out the running activities on the port.
just change the port number for any other port number.
This is the output you get after running the commands.
I am using Git bash on windows.
For me, this works every time (macOS): sudo kill -9 $(sudo lsof -t -i:3000)
Simple and Easy solution
close your current terminal and open a new one.
If you are running it in VS Code just create a new terminal and delete the old one.
On React - you can run an already created React single-page application (SPA) by
npm start command.
That may start your locally hosting development server and runs your app at:
http://localhost:3000/ which is equivalent to: 127.0.0.1:3000 address
127.0.0.1 is the default localhost IP number while the default port number set by
create-react-app package is 3000.
When getting: “Something is already running on port 3000" failure error message you may think that the port captured by another process running on your machine but you’ll find that it is captured permanently as if it runs on 0.0.0.0:3000 address
Solution:
In your project libraries created by create-react-app script navigate to:
node_modules/react-scripts/scripts/start.js
While running npm start command - the start.js script is being called and executed
There at start.js file in you editor find the above line:
const HOST = process.env.HOST || '0.0.0.0';
and change it to:
const HOST = process.env.HOST || '127.0.0.1';
save and run your web app again at: http://localhost:3000/ or http://127.0.0.1:3000

cound't start express app using pm2

I do nodemon it's fine but too bad nodemon isn't restart the app when something happened.
Where's what i've tried in my ubuntu server :
npm install pm2 -g
go to my app directory and do pm2 start app.js
pm2 startup ubuntu
My app still has 502 bad gateway error. Sigh why does this happens? I do nodemon it's running.. Strange thing is in the terminal pm2 did show an app and it's online :(
1) Check if your application is listed in PM2, use the command pm2 list
2) Running apps should be listed, you can either find an online or errored
status for each app
3) If your app is online, try to curl localhost:port ( if this works, something is wrong with your firewall or nginx / apache configuration)
4) If it is errored, please check pm2 log - ~/.pm2/pm2.log'
If pm2.log shows "... had too many unstable restarts ...", try the solution https://stackoverflow.com/a/30651668/3054511

Forever and node.js, stopping one forever process stops all processes [duplicate]

I am deploying two node.js apps on the aws, the two apps are in the paths shown as
/home/ubuntu/nodes/app1/app.js
/home/ubuntu/nodes/app2/app.js
respectively
to run the node.js apps in the background, I used forever to start two apps, so like
$ sudo forever start /home/ubuntu/nodes/app1/app.js
$ sudo forever start /home/ubuntu/nodes/app2/app.js
so forever works well by running the two node.js apps in the background process.
However, when I tried to stop one process with forever command like this.
$ sudo forever stop /home/ubuntu/nodes/app1/app.js
unexpectedly, both node.js process are closed with info like this
info: Forever stopped process:
data: uid command script forever pid logfile uptime
[0] r2pZ /usr/bin/nodejs app.js 24852 24854 /root/.forever/r2pZ.log 0:0:1:14.775
[1] 9f2h /usr/bin/nodejs app.js 24870 24872 /root/.forever/9f2h.log 0:0:0:58.733
I assume it is because two node.js process has the same name - app.js, how to avoid this by close only one process
You can use an uid (see here):
$ sudo forever --uid "app1" start app.js
$ sudo forever --uid "app2" start app.js
And to stop:
$ sudo forever stop app1
Update
The --uid option is deprecated.
Now you can use the --pidFile option. Example:
forever start --pidFile /some/path/app1.pid app.js
forever start --pidFile /some/path/app2.pid app.js
And to stop:
forever stop --pidFile /some/path/app1.pid
You can kill only one process using the index of the process shown in the forever list command. For example, if you type forever stop 1, only the process with the index 1 will be killed

Categories