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:
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!
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:
scp -r [localfiles] [user@remoteServer:[destinationFolder]]
A real application of that would be:
scp -r ./video-stream-sample [email protected]:video-stream-sample
git clone [url]
In my case I will clone the repo of the article I introduced earlier with:
git clone https://github.com/daspinola/video-stream-sample.git
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:
node server.js
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.
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:
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!
Article 7 of 30, part of a project for publishing an article at least once a week, from idle thoughts to tutorials. Leave a comment, follow me on Diogo Spínola and then go back to your brilliant project!