When you want to test in a real production environment instead of using your computer as a server and for the deployment of your Node.js application you usually have to use a server like the ones available on Amazon or DigitalOcean.
This is a basic example and won’t touch on how to set up DNS, SSH or reverse proxy. As an example I will deploy the sample of my last article of Node.js video streaming using DigitalOcean.
Disclaimer: I’m not being sponsored in any way, this is the service I decided to use given the price and reputation.
They make available servers in the cloud that they call droplets which have a pricing of 5$ for the lowest tier (which is more than enough for tests and small traffic websites).
When you create a droplet (server instance basically) you are billed 0.001$ when you create a droplet and for each hour the droplet exists (even when stopped).
Creating the server is as simple as:
- Create an account with valid paypal/credit card here;
- Choose “create droplet” where you will have to at least select the system distribution (ubuntu, fedora, Debian…) the billing plan, datacenter region and hostname (you can select environments with certain programs pre installed, this example starts from scratch);
- After hitting create it shouldn’t take long to have the server instance up and running, fot this example I will be using Ubuntu;
- After it is deployed you will get an email with the IP, user and password of that droplet, open you terminal and type:
ssh root@[server IP]
This will prompt a (yes/no) just say yes followed by the user and password. Note that on the first time connecting you will have to give the password in the email twice followed by a new password
(current) UNIX password is the one you just typed!
- And it should be on your clean server running ubuntu, you can also access it via the browser in the droplet options
- To install node I usually use nvm, which allows me to switch between node version quickly so just download it with:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
You may need to reload your bash with exec bash to run the install of node with
nvm install 8.2.0
And with this you have a server, somewhere in the world, with Node.js available. Still no files and nothing we can do it tough…
Note: To avoid being billed when just playing around with your servers can create a snapshot of your droplet (it has a cost associated but it is usually less than if you stay with the server open 24/7) and destroy the existing droplet, when you want to come back just create a new server with the snapshot.
To get your project in the server you have some options like:
- Filezilla — is a FTP software that allows you to access your server folders and upload/download your files to it;
- Scp — using the command line you can have transfer your files by using:
scp -r [localfiles] [user@remoteServer:[destinationFolder]]
A real application of that would be:
scp -r ./video-stream-sample firstname.lastname@example.org:video-stream-sample
- Cloning a git repo:
git clone [url]
In my case I will clone the repo of the article I introduced earlier with:
After changing any “localhost” in the code to the server IP and following the repo instructions when you try to access from any place the link “http:\\[serverIP]:3000” the sample will be working as intended!
Managing with PM2
You may ask “I want to continue to make changes without opening another instance of the server on my local computer but since node is running I can’t do any more commands”.
To avoid that you can use pm2 to manage your node instances while you keep on being able to run commands, usage would be:
- npm install -g pm2 (-g to set it globally so you can use it in various projects without adding the dependency)
- Instead of running the node application with something like
You instead run it with
pm2 start server.js
The result is something like this
This way you can keep managing your server while your application runs on the background.
Whenever you want to see what is running you can run pm2 list and to remove a process do pm2 kill. You can find what is available of PM2 here.
You should also configure
As stated before this is just an introduction to how to setup your application in a remote server instead of your own computer, DigitalOcean sends you an email with security stuff you should also configure and to send to production you shouldn’t leave the IP exposed so a DNS and reverse proxy and surely mandatory to avoid problems. Some of the configurations would be:
- SSH key, more secure and you don’t have to introduce the password in every connection (check it here);
- DNS to have a domain to access instead of the IP (check it here);
- Reverse proxy with nginx.
Hopefully this will help in the deployment of your applications making you a step closer to shipment. Already using another service or would like an example of the last steps that where refer but didn’t talk about let me now!