I m new on both sails.js and node.js.
I created a simple api with sails.js. It works fine when I start it with sails lift --prod. On the other hand when I start the application with forever start app.js --prod and it always returns 0 altough I dont not get any error and warning and I can see my app.js in the list with the forever list command.
I created a simple node.js application to test forever and it worked as expected.
I tried to run my Sails.js application both my local windows and linux/ubuntu machine on digital ocean, they worked fine with sails lift but not forever. Also I tried node app.js --verbose and it was ok. My sails version is 0.11.4. Thanx for your help.
Well eventually I start my application with forever. Those are what I did
I checked forever commands and saw logs command which provides running forever applications log files.
When I checked the log file under c:\users\user-name\forever I saw a warning about Sails migrate settings should have been done.
Then I read this documentation about Sails migrate settings and I set my project as migrte: 'safe' .
Now it works fine.
Your error is similar to this: Cannot run with forever js
I think, you should open config/models.js and add migrate: 'safe'.
Related
I have spent multiple hours to try and get my server up and running. As a part of a class assignment we are migrating to ExpressJS. So the instruction go as follows:
Install latest version of nodejs and npm as well as express, morgan,and forever in our ssh server(Ubuntu based).
In our "website" folder we created a "public" folder to throw all web content in.
We then downloaded a server.js script the professor uploaded and we are to place it inside the "website" folder we have.
Then we set the path to the "website" folder. To be clear "website" contains the "public" folder and server.js script.
Write `forever start server.js` in the terminal
Forever processes server.js , but I do not get my website loaded,no matter the browser I access through(the terminal is also open). I tried writing forever list and all processes are stopped. I think the issue might be around here.
I have tried using:
forever start ./bin/www -did not work
sudo forever start server.js-did not work
sudo nohup forever start server.js-did not work
nohup forever start server.js-did not work
I really need to figure how to get this server up and running. If you need something from me let me know what and how to get it, please. I am a windows user and more than half of these commands I found researching other questions. If you need logs, or images also, please let me know where to find them to provide them to you since I have little technical knowledge for some of these things. - Thanks!
You can try nodemon for dev. or pm2 for production if you're trying to keep your server running.
https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
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 have a node app locally and I normally run it using the run button in the web storm, which works fine. While I am documenting the project I came across this blocker: if I try running the same app through the terminal using the command: node app.js , the server starts but the browser throws and error stating "This site can’t be reached" "localhost refused to connect".
Because the port is occupied.
You can change it using app.listen({port})...
or
linux:
export PORT=4500
node server.js
windows:
set PORT=4500
node server.js
I found this myself.. since I used the scaffolding app and it stores info related to server at bin/www, so we will not be able to run the app using the command : node app.js but instead we could run the app using nodemon(which I installed globally on my machine)
Love nodemon for this awesome feature <3 :)
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
I have a SailsJS app set up on a Webfaction server. Everything works nicely (site can be accessed through browser, console works) when I run the app via any of the following commands, with and without the --prod param:
sails console,
sails lift,
node app.js
However, when I try to run the app with forever using forever app.js I get a 502 error, as if nodejs server isn't even running. When I run forever list I can see app.js listed among running processes.
How can I have my app run with forever?
Forever is considered outdated by many in the Node community, and thankfully, has been replaced by several other fantastic (dare I say, better) tools.
If you're running a newer flavor of Ubuntu, you can always install systemd and kick off the application that way. If you're seeking something more streamlined, Phusion Passenger might be your ticket. It has a long track record of successes, and I wouldn't hesitate to toss it into production.
I managed to solve this issue; the problem occurred due to SailsJS migration prompt which shows up when you start the server. Running app.js with forever worked, but the server didn't start because the script hanged waiting for a prompt reply. If you encounter this issue just make sure you have your migrate option set in model config to avoid running into migration prompt.