Programmatically watch and restart node server - node.js

I was getting tired of using FileZilla every time I wanted to push a change to my server hosting my website and so I set up a github repo and linked it to my server so that changes and pushed right to the server.
However, my backend is written in node and so each time I update my server.js file I have to restart the server. With "node server.js"
Is there a way to watch the file and programmatically restart the node server when an update is detected?
If it helps, my serve is Ubuntu Linux running apache2

You could try writing something yourself, or use one of the popular libraries that are already out there:
https://github.com/petruisfan/node-supervisor supervisor server.js
https://github.com/remy/nodemon nodemon server.js

Related

React JS Front-end & Node js Server deployment on shared hosting

I am working on react js project & using node js for backend(to handle DB as well as to run CRON JOBs). At my local machine I had created 2 folders "botclient" & 'botserver". I starts the server using "node server.js" in 1 command prompt & in another command prompt, I satrs client using "npm start". Now in browser I use "http://localhost:3000 & my application works.
Now my client has given me 1 shared hosted domain something like http://mybot.hismaindomain.in.
Now he has given me cPanel credentials to deploy this app. I have no idea how should I deploy it on server. I had tried lots of threads like "running nodejs react in one directory" or "react js nodejs deployment on hosted server" But I could not deploy the application on Hosted server.
I am total newbie in this technology so please help me to solve this problem. Whether I am doing wrong folder structure? Whether I need 2 sub domains?(for client and server separately)
Please help me. Thanks in advance.
(This should be a comment but I csan't comment yet)
Maybe try using cPanel cron job to run the node command
You do not need two sub-domains. Neither do you need a specific folder structure to have shared hosting.
To run node two or more servers on the same host, from the same repository, from just one command line, you can use a package like 'concurrently' or 'npm-run-all', which lets one npm start-command activate a number of processes in parallell. For example: concurrently \"node server.js --port 4000\" \"webpack --config client/webpack.config.js --port 3000\" \"node cron-jobs.js\". In this example, the server is in the project root, and the react app is in client subfolder with its own package.json.
Then, to share a domain it is necessary to setup a reverse proxy, so that the backend's port can be accessed via a designated path, ie: /api. There are many ways to do it and there are others who would explain it better than me. Try google react node config reverse proxy or similar. Good to know is that the easiest solutions might be limited to a dev server, while the more complicated ones (like nginx) are independent on your tech stack (which you might not need).
For cron jobs, you could try a package like 'node-cron', check this guide for the basics

Is there a command that allows you to restart a pm2 app if a file in another directory is changed?

I have an Ubuntu server that is running plesk obsidian to manage my websites, on the same server I have a nodejs application running with pm2.
There is a json file that a website and the application needs to access. I am able to make the app's code point to the file and access it, but when the website makes changes to the file it doesn't automatically restart the nodejs app as the file isn't in the same directory.
Is there a command that I can use to make pm2 check for changes with the json file and restart the app?
yes there is a command
pm2 start app.js --watch
here app.js is the file name which cause pm2 to restart

What's the equivalence of "Node app.js" on a file domain?

On localhost all there is to do is type "Node app.js" to run node locally, but I am trying to do this on a domain that I do not own but has Node installed. "https://www.'example'.com/MyID/myapp" is the example I will be using, do note that I only have access to edit everything in "MyID" file and not "https://www.'example'.com". the problem is that I do not know how to run node on the domain. The IDE I'm using is codetasty and it does not say how to run node on their sandboxes either.
This is a tough question because there are a lot of misunderstandings here. Node is a process that runs a JavaScript file. You can access http://localhost like a website because your node app creates a server that listens to a port and responds to the HTTP requests made by your browser. It seems like you are asking how to run node on a file hosted online. That may be possible but I think you actually want to run the node server on your web host and then connect to that instance with your browser. Most webhosts don't allow you to run node so you may need something like digital ocean to do so.

Do I need to manually restart my app on EC2 if the server restarts?

This is my first time with EC2 so keep that in mind. I spun an EC2 instance and put a really basic nodejs/express app up on it. I connected to the ec2 server via the terminal on my personal computer and ran node app.js to start the app and everything is running fine. The part I am confused about is how long this will run for. Ideally, I just want it to sit there and not touch it and have it run for hopefully years. Will it do this? If not what do I need to do? What if the server restarts for some reason? What is the common practice here?
Go to root directory of your project and type this command to run the server permanently.
sudo npm install forever -g
forever start -c "node app.js" ./
This blog may be helpful, in setting up node for production environments

Combine Yeoman Server and Node Supervisor

I'm using Yeoman to develop the frontend html app (backbone and bootstrap) and would like to use the same folder for backend development for the api (node, express, mongodb).
What I would like to do is to have the browser refreshed no matter what file was changed on frontend or backend.
What I'm doing now is:
For Yeoman I'm using the "yeoman server" that would refresh the browser every time I change something in the app folder.
I'm using the node supervisor module and executing the "supervisor server.js" for automatically kill the server and relaunch the node server if file is changed on the backend.
I'd like to avoid this as I need to run the yeoman and node server on different ports.
Is there a way to force autoreload of the browser with node supervisor or use the yeoman server as a classic node server?
You should be able to force the browser to reload with command grunt reload. Just childProcess.exec or childProcess.spawn the command.

Resources