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. :)
Related
I have a sailsjs app on AWS EC2, which I have been running till now using forever. I have two adantages using forever:
1) Perpetuality: I can use the CLI forever start app.js or forever restart app.js and then app starts running and keeps on running till I stop it with the command forever stop app.js. So, the app does not stop even when I close my terminal. The process keeps on running.
2) Runtime Log: I have a .forever directory that has a log file, while on real time records the server logs, and when I check the log using tail -f file_name.log, I get to see run time logs.
However there is a disadvantage: Every time I upload a new/modified server file, I have to restart the app manually. To get rid of this, I am switching from forever to nodemon.
From the documentation provided by Nodemon, I cant figure out how can I replicate the two advantages, as mentioned above, from Nodemon too. Will be a great help if anyone can guide me on how to start my nodejs app using nodemon so that it can keep running even after closing the terminal on my side, and how to watch runtime log of server.
Just my two cents.
I use nodemon daily while developing and I dont think its something you want to use in place of something like forever. Nodemon is used when developing, the software will detect when there has been a file change and restart the server but for deployment it should not be considered.
There is no need to change either because forever has this use case handled with the --w or --watchDirectory comand that will watch for file changes(It can be found here on their readme).
I wasn't quite sure what to call this question but here i go:
i have a remote server where i have installed node.js now normally this would be how i start the server:
ssh root#ip
cd /var/www/mydomain/server
nodejs server.js
This works without any issues however what happens when i close down the terminal? How can i make sure that the server doesn't just stop. And how can i control it after i have started it (for instance restarting / stopping it).
There are plenty of solutions here, but maybe the most easy to start with is using forever.
Forever is a npm module that keep your app running and restarts it if it crashes.
Also there are more advanced solutions, like using PM2, which I recommend, but first take a look at forever.
I'm new to aws and recently I have been able to install node, mongod and also, FTPed project file to the server.
For mongodb,
I'm doing mongo in a separate terminal tab and starting service in another tab. I want to know how can I keep the mongo service running.
For node app,
Right now i'm doing node app in the server. How can I keep it alive too ?
Now the problem, is I open the browser with publicip:portno but nothing happens. How can I locate app and run it in the browser.
my app structure is
/
node
mongo
server.js and app related files
Using the Linux/Unix nohup command allows you to start commands that ignore the signals associated with the controlling terminal process terminating (SIGHUP). Adding the & to the command allows that command to run in the background and sending the output to /dev/null will ensure that your disk does not fill up with unnecessary log output. Here are some commands that should work:
nohup mongod >/dev/null &
node server.js >/dev/null &
This is more of a Linux/Unix command line issue I think. You can use the node module called forever to run your Node.js process in the background easily.
npm install -g forever
forever start YourScript.js
You can place an & at the end of the mongod command to place it in the background.
Ensure that in your node app you have the command app.listen("port number");
This is the "port number" that you should be using in your browser to render the page with the elastic IP from your AWS instance. Make sure that your elastic IP is configured to accept inbound requests.
To keep the service/app running in the background you can run the screen command then launch ur app/service (e.g. mongod OR node app.js). In the same terminal that your app is running press control + a + d, you should see
(detached)
printed on your screen.
This should keep your app/service running in the background.
As you can tell by my question, I'm new to this...
I built my first website, I set up my first Node.js server to serve it and then pushed everything live on EC2.
I tested everything on my EC2 IP address and everything seems to be working.
Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.
Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.
So my question is how do I keep my Node.js server running on EC2 for like... forever..so that my site stays live.. forever :)
I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system ?
I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests) ? And if that's the case why do we need extra modules/settings to achieve that ?
Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)
EDIT (a few details you might need) - After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :
I type ssh -i xxxxxxxxxxx.pem ec2-user#ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the
terminal
After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
There are 3 folders in the machine : node, node_modules and *name of my app*
app.js resides in *name of my app*
After that, the site goes live on my EC2 IP
Once I close the terminal, everything is switched off
Before you invoke Node.js, run the command:
screen
This will create a persistent environment which will allow your process to keep running after you disconnect.
When you reconnect, you can use this command to reconnect to that environment:
screen -r
Here's a random link to learn more about screen:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
However, this won't help you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which includes adding something to /etc/rc.local.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.
https://www.npmjs.com/package/pm2
This is so easy to use.
$ npm install pm2 -g
then just start your app with pm2 like this
$ pm2 start app.js
In the above link you can find diferents tasks to perform if you need.
Hope this help the newbies like me.
There's a better way. Use forever.js.
See it here: https://github.com/foreverjs/forever
This is a nice tutorial for how to use chkconfig with forever on CENTOS.
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
Or use tmux
Just Enter a tmux screen run node server
Ctrl+b Hit D and you're done.
I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.
follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):
npm install pm2 -g
pm2 startup systemd
See the output and at the last line it must be like..
You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup
systemd -u sammy --hp /home/sammy
Take the last line command and run again with root privilege.
(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")
npm run pm2-start
for more info follow the link.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
If you are using a Ubuntu EC2, better to use the following we have been using this for the past 6 years and have had no issues with this.
sudo npm i -g forever
Now start your main, example
forever start index.js
forever start src/server.js
To stop the server use the following command
forever stop index.js
To list multiple servers running forever
forever listall
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.