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

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

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?

Installed JsZip but its not working by grunt

I used https://stuk.github.io/jszip/ on my local machine and update all dependency by npm and used command grunt connect but this only showing web server started but on this url its not working.
$ grunt connect
Running "connect:server" (connect) task
Started connect web server on http://localhost:9999
Done, without errors.
even i have tested the app there is no error,
can anyone let me know how to get such type of projects from git-hub and run locally by npm or composer/bower.
I have used simple javascript instead of going with NodeJs and here my code is :
Here is Gist Code you can run.

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

ruining EasyRTC server using iisnode

EasyRTC is one of the best available opensource WebRTC solutions that runs in node.js! I have manged to run it on node.js on my system but my application is in Asp.net so I've been trying to run it using under IIS using iisnode! but for some reason I haven't been able to make it work! running this application in node.js i use node server.js to start the server but in IIS when i set the default page to server.js to start the application it wond work!
this is result by using
nodejs:
nodejs result: (working properly)
IISNODE Result:
looks like it's not even running it !
This error is due to lack required module for easyRTC. type in node command prompt "npm install" It will install all dependency module. And after that try to run server. I hope It will work for you.

Resources