I am about to try it, so any tips? Thanks
NodeJS app running on Forge?
Has anyone tried running a node app on Forge? Would it simply work by setting the deploy script to boot up the express-server in the background?
I haven't (yet) registered to Forge, so that's why I'm asking here.
I see Jacob here had some problems with node earlier.
You should be able to just SSH into the newly provisioned server and just setup the nodejs server. I am sure you can find a detailed article from Digital Ocean on how to do it.
If you create the site within forge from a github repo for instance. Then you can set a daemon at server level. 4th tab in my mind. You can type a command like "node /home/forge/yoursite.dev/serve.js" just like you would run it on your local machine.
Supervisord will keep it running for you. With (auto)deploy just kill the process, and it will restart again.
Would anyone have the deploy script required to restart a nodejs app on deployment?
bit late, but you can kill the process/deamon running the app. If you make a supervisor script for the node app, you can kill the app by name
Hey, just went through this myself for the first time, but in the end it was pretty straightforward - just setup the site on forge (I used 'standard html' type), connect GIT repo and deploy your code.
Then in NGINX configuration you have to change location / block to be proxied to your node server as so:
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
You can test-spin your server by SSHing there and calling node path/to/server.js. That should allow you to access your app through browser.
After you confirm this works, just add the node command as a daemon in Forge. Go to Server > Daemons and add a new command as follows:
node /home/forge/yoursite.com/server/server.js
This will make sure your server keeps running and is automatically restarted in case of error etc.
Additionally I use autodeploy with bitbucket, below is my deployment script. Notice the last line that restarts the daemon, it uses pkill -f to kill process by full command name. This was the easiest I came up with as you don't know PID here:
# pull code
cd /home/forge/site.com
git pull origin master
# install npm modules for server
cd /home/forge/site.com/server
npm install
# restart node daemon
pkill -f 'node /home/forge/site.com/server/server.js'
@jan_zikmund I know this is an old question, but your answer still has value. The only thing I had to change to get my deployment working was to restart the node daemon this way:
# restart node daemon
sudo -S supervisorctl restart daemon-1111111:*
where 111111 is your daemon id from the daemon section of your server.
@jan_zikmund did you have any issues with Forge starting this daemon? I am able to get the node server running via SSH, but when I put the same command into the daemon, I get this error: "daemon-255693:daemon-255693_00 FATAL Exited too quickly (process log may have details)". The log doesn't have any additional info.
Another way to consider is using pm2, which Forge installs on the system by default as well. There is a mode which will monitor the Node processes in case they fail, it will auto restart. That appears to be the more common way in the NodeJS community, though I suspect that Supervisor would also handle the job well.
@rickschmidt - no, sorry, never had this kind of issue, the demon starts it correctly for me. Only issue I had was I had to configure Directory (Optional) to my server directory when creating the daemon, otherwise the dotenv config file wasn't read. But other than that no problems.
hi in case anyone still looking for answer for this one, now Forge has Node.js installed automatically when you select server type: App Server.
Please or to participate in this conversation.