How is a node application run when it is being deployed? - node.js

When you are writing a node application on your computer, you can run it from your computer by typing in node . However, how does that happen when the application is being deployed.

When you are writing a node application on your computer, you can run it from your computer by typing in node . However, how does that happen when the application is being deployed.
Basically the same way: there's not much of a difference between your computer and a computer in a data center. Either way, you start the process and it runs. It binds to a port that must be open, and then some firewall must be configured to allow incoming connections from the Internet to reach that port.
How you start the process in your "deployed" application varies greatly and is up to your needs and taste. You can:
1) Provision a server somewhere, install node on there, put your code there, and then run your application. You run it in the same way as your local computer. Log in to the computer and execute node. Or you can set it up to run with a process manager.
2) Use a PaaS like Heroku.
3) Use a serverless environment like Google Cloud Run.
You have many options and I'd explore them all!

Related

Google VM - process persistence

I have a Google VM, and i can start a web server. The command i issue is: python server.py.
My plan is to have the application running.
Since i will eventually close my pc (and thus the terminal), will the application continue running normally?
Or, do i have to start the server and then use disown, to make the app run in the background?
NOTE: If the second option is the case, does this mean that when i re-log in, and i want to shut down the server, the only way to do it is with pkill?

Keep node in running state even after user log-off

How to keep a node application running in windows even when user logs off?
Also how to keep running a node http-server even after user log-off?
You have 2 great options. One is as mentioned in comments above Forever.
The other is PM2 which is easy to install and offers an incredible amount of options. I use this in all projects, but I cannot attest to the Windows version as I am on Linux & Ubuntu servers and work on a Mac. You can daemonize your node process, follow logs, cluster it and make sure the process reboots even with a server shutdown (it is a service).
windows task scheduler: execute node.exe: start in project folder: and argument (app.js)

How are most Node applications started?

I know to start my Node app I type in the Win shell, node app.js.
But this is obviously not how a webhost would maintain a Node webserver (ie what happens if there is a power outage, a Node exception, etc).
I see things like Forever and running Node as a Windows service, but I feel like the creator of Node must have had a different idea? Something like Apache is installed as a Windows Service so that it runs even if the server reboots - what is the correct method of doing this for Node? I don't like the idea of introducing another piece of software just to keep the server going.
Thanks.
The problem is that many systems do not do that. For instance MongoDB doesn't even run like that on windows.
The best solution I have found is this https://nssm.cc/
Also you have to consider even on Linux you need to run something like upstart to keep node programs running when you close the console.

NodeJS app running on VPS (linux) crashes when I put my laptop to sleep

I absolutely 100% new to VPS and linux as of yesterday, and I'm running into an issue. Here's the process:
I SSH into my VPS box in OSX terminal. The VPS is running CentOS 6 for what it's worth.
I navigate to the correct folder and I run node app.js to launch my app in NodeJS/ExpressJS.
App launches and is readily accessible via the web at my VPS' ip address + the allocated port number.
If I put my laptop to sleep, the app crashes and is no longer accessible via web.
Again, being new to Linux I'm not sure how to solve this problem. It makes sense, as the terminal that was running/taking logs of the node app is no longer responding, but what I'd like is:
a) To be able to start up the app remotely then have it just...run...forever, until I manually stop it
b) To be able to SSH back into my server intermittently to check the logs, either via my mobile phone or my laptop.
Are either of these two things possible? Clearly my protocol of launching the app via terminal (as I'd normally do if running it locally) isn't the correct way to do it but I'm having trouble finding resources telling me what to do!
EDIT: I'm actually using Node Supervisor to run the app which helps keep it up and running when things crash, not sure if that affects the situation.
Most probably your app is printing to standard out (the console), and that stream is closed/broken, when you put your laptop to sleep.
There are at least two options:
Use screen: Just type screen before starting your app. Then start your app. Then Ctrl-A-D to detach from the screen. You can then safely log out from the VPS and put your laptop to sleep. To go back to the output of your app, log back in and type screen -r
Run the app in the background: node app.js &.

Dev environment for multiple server setup - Nodejs

This is my first time building out something with multiple servers. I wanted to know if anyone could point me towards a guide for setting up a dev environment (windows) for a backend that will be set up on multiple servers ie one server for the API, one for another set of processes (ie file compression) and one for everything else.
Again, just trying to figure out if it's possible to set up a dev environment to test out the system on my local machine.
Thanks
You almost certainly want to run virtual machines (on something like VMWare or VirtualBox) to really test multi-machine stuff. However, I also develop for multiple machines every day (we have an array of app servers, an array of background worker servers, e-commerce servers, cache stores and front proxies—and I still just develop on one virtual machine that has all that stuff running on it. Provided you make hostnames and ports configurable for everything, there's not much difference between localhost port 9000 and some.server.tld port 8080. Actually running all the VMs on a single computer would likely be painful, both in terms of system resources and complexity.
There are tools to help with setting up VMs with similar or the same configurations too. Take a look at http://vagrantup.com/ and also http://babushka.me/.
Just my $0.02.

Resources