Run React dev server as background process? - node.js

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?

Related

How to run and work with Node.js on my remote VPS

Maybe it is dummy or noob question, but this is the first time I am trying to run / deploy my local Node.js application to the internet via my VPS hosting.
When working locally, I am using nodemon package to auto restart the server on each change.
Also, when turn off the terminal or shut down the Mac, the Node.js will be shut down too.
How these things could be handled via a remote VPS with a public domain?
Btw, another thing is how to compress/compile/hide/secure my client side code so it wont be able to be read or something.
I am asking this because when I am doing source code, It is very clear to see the whole app requests and therefore "play" with this.
Thanks.
check out pm2 (production node manager)
you can install like this
npm install pm2 -g
so instead of nodemon app.js you'll run
pm2 start app.js
instead. This keeps your app running even if you restart your server
check out the official site for more info on pm2

how can I debug a node app that is started via the command line (cli) like forever or supervisor?

I'm familiar with debugging my own node apps (usually with node-inspector). Today I'd like to debug someone else's program. I'm trying to track down an issue with supervisor. So naturally I just add a --debug (or debug-brk) to the command call, but it passes that to the code that it is supervising.
I've tried adding debugger lines to the js file for supervisor but that didn't work (probably because no debugger was attached at that time). There's a bit of a race here -- I need to start the debugger and attach it to the supervisor process after it starts but before it processes its arguments from the command line.
What I really want to do here is stop supervisor and debug it before it processes its command line arguments. How can I do this?
I had the same problem while developing my hexo blog. The documentation isn't all that complete yet so I find myself needing to reverse engineer at times.
The basic idea is that in Node.js even your cli apps are simply normal node apps that you are exposing to the OS command line interface. On Unix systems you are using this line:
#!/usr/bin/env node
To allow the environment to execute the script.
Many cli based node apps try to insist that you install them globally with the -g option.
npm install -g node-inspector
I personally prefer to have as much control of my development environment as I can get, so I prefer to break some conventions and check my node_modules in to source control along with installing everything I can locally by dropping the -g.
npm install node-inspector
Now you don't have to do this in order to make this work, I'm just describing this setup because it relates to your question. When I run node-inspector I can't simply use:
node-inspector
Instead I must explicitly invoke it from within my project. I do this by executing the symlink in my node_modules/.bin folder:
node_modules/.bin/node-inspector
Now I'm running node-inspector just like you.
Next all I need to do is start the cli process in debug and optionally pass params to it:
node --debug-brk node_modules/.bin/hexo generate
Note I am explicitly calling the symlink here and not simply:
node --debug-brk hexo generate
If I tried the line above I would get an error: "Error: Cannot find module".
I hope this helps.

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.

Run node.js package on web server continuously

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

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