Run node.js package on web server continuously - node.js

I'm using a blog platform specter that starts when I run npm start. Only when I have run npm start will it show up at the site URL. If I do command-c in the terminal, it quits running the package and the site goes down until i run npm start again. Is there some way to set up the server to keep the site up continuously? Right now I have to take down the site before I make any edits and then start it back up with npm start.

One thing that you can do is to install Forever by Nodejitsu. What this does is it runs your node script on the background "forever" or until you stop the process or set a timeout limit.
To install Forever, just do:
npm install forever -g
For your case, you will want to cd into the directory that Specter resides and start forever:
cd $(specterLocation)
forever start server.js
This will start your server on the background until you do:
forever stop server.js (in directory of Specter)
forever stopall (wherever on server)
To see the list of processes started by Forever, just use:
forever list
Hope this helps!

You may want to take a look at nodemon
It will auto-restart your node application whenever it detects changes to the directory where nodemon was started.
It is available as an npm package as well (https://npmjs.org/package/nodemon).

Just make your edits with the site running. Then when you're done, stop the site and start it back up. The site isn't continually running from the files, it starts up, loads into memory, then runs from there, releasing it's hold on the files.
Or, there are any number of more robust management strategies you could implement, complete with version control, process managers, integration strategies... If you plan on growing your skills to support high volume or commercial implementations, then you should look into these things. If you're just running your personal blog and that's it, just edit your files, then restart your server process when you're done.

Use Node Supervisor. It's really easy to use and install.
npm install supervisor -g
run with
supervisor whateverFileYouWant.js
https://github.com/isaacs/node-supervisor

Related

Run React dev server as background process?

I've been recently experimenting with React and I want to be able to run the dev server that's initialized when you run npm start in the react app's directory as a background process on centOS so that I can continue to use the command line.
Is this possible with something like pm2? I tried various commands but couldn't get it to work, of course I can do the following "nohup npm start &" but I would much rather have all my managed processes using pm2 or some other process manager maybe?
Any advice?

Why server restart is needed in Node.Js for every change?

I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.

Running hapijs as deamon

How can I run hapijs as a server deamon on a Linux box? Right now, I'm running it as a user process for development with the node index.js command for the main page, but in the long run it should be www-data or whatever else user that runs the process.
If you want to run node as a daemon without any extra tools, you can use nohup:
nohup node index.js &
However, the following tools can do this and also have some other really useful features such as automatic restart on exit, log redirection and in the case of PM2, clustering:
PM2: https://github.com/Unitech/pm2
Forever: https://github.com/foreverjs/forever
If you want your service to start when your machine start/reboots, you can use something like Upstart (on ubuntu) or System-V:
https://www.digitalocean.com/community/tutorials/how-to-write-a-linux-daemon-with-node-js-on-a-vps
To run as different user to the user you're logged in with:
sudo -u somebody node index.js
Please note that none of the above is specific to hapi but rather applies to any Node.js app.
PM2 is the best option hands down. It scales from local development through to production without issue.
First Step:
npm install -g pm2
The -g flag is simply for installing to globally so it's available as system command.
Second Step:
pm2 start index.js
The start command simply replaces node index.js Behind the scenes it runs the node process but as a daemon.
PM2 Actual Use Case
cd projects/my-app
npm install -g pm2
npm install
NODE_ENV=development pm2 start index.js -n my-app
pm2 stop my-app
pm2 restart my-app
pm2 status
pm2 logs my-app
pm2 m
Those should be enough to get you going. The nice thing about PM2 is it works great in a CI/CD environment as well since you can recall process by name. Finally out of the box it does log rotation and a few other awesome things to keep you going even if stuff goes south. Apps will also auto restart if they crash (obv. configurable).
Additional configuration allows PM2 to watch files on disk and restart the app as they change. This is great for development as you can code + save files and the API you're building in HapiJS will simply restart and your changes are live.
I use supervisord and it works great.
In short you have to configure supervisord to start your hapijs application. In addition you need to configure nginx or apache to reverse proxy requests to your hapijs application.
You can find detailed instructions on set up at http://blog.risingstack.com/operating-node-in-production/
It feels odd to suggest a tool when you haven't explicitly asked for one. nohup-ing the process and running in the background is an option that requires no new tooling, but for what it's worth, I would suggest Docker-izing the application and letting docker handle everything. Docker has several features built in, and even though it is not just for creating a daemon (it does so much more), you can use it's restart='always' feature to keep a process running.
Hope that helps.

How to make Hapi auto reload app during developing

I'm new to *Hapi *framework. During development, I have to restart the hapi server whenever I made any changes to the code.
For the view part, I can add an option {isCached: false}to make the view read the latest html file every time. However, is there an easy setting to make it reload code automatically whenever it is changed?
UPDATE:
Thanks to dylants' suggestion, Nodemon works great.
However, in my app there is a selenium-standalone child process, whenever the nodemon restarts, it will generate an error log. ...Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again...
I have tried https://github.com/remy/nodemon#controlling-shutdown-of-your-script, it doesn't help.
I've used nodemon. You just start your server with $ nodemon instead of $ npm start and every time you make a change to your server code it restarts the server.
I have found node-dev to work well for me.
npm install -g node-dev
I personally prefer pm2 to achieve this.
pm2 start app --watch
More info about pm2: http://pm2.keymetrics.io/

How to edit and deploy code without restarting server?

I have node server which i run using forever. But each time if I edit my code I'll have to restart the server. I came across the module called hotnode which can perform live edits but will it have the same performance as the forever module or can I run my code using both the modules. I am confused. Any help wil be much helpful
Have a look at nodemon.
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
As an alternative to nodemon you can use node-supervisor.
I used to use nodemon, but for some reason it didn't detect code changes on my linux box, which supervisor did flawlessly.
The downside is that it doesn't (or at least didn't) give the colorful output nodemon gives.

Resources