October 5th
New York City 2017

#40 serverBasics flyBy

What is this for

Learning web development in this day and age is something that lots of people are already doing, but we only know half of it. We only dive into the client-side end of the deal, so we know how to use HTML and javascript, but server-side is never mentioned.

So this is what this is for, what is a server, what it does and why should you even think about them.

Let the nerds take care of that

To that I would say no, a server is basically a computer somewhere storing information that when requested, gets ‘served’ to the client. That is basically how the internet works, A bunch of computers in places giving you stuff when you ask for it.

A fairly easy action that besides its value most people that work in projects that require a service like this don’t really know the basics of it.

So here we are, trying to tell you what this is and how to set up an environment for you to play around with all this.

Digital Ocean

Either you left cause you don’t care about this or you wanna work in this soon to be nightmare.

So lets dive into digitalOcean (get it?).


A droplet is virtual computer. An emulation of an actual computer set up inside a real much more powerful machine. this allows getting your hands into a computer very fast and easily. In this case one with a public IP and and a easy access from anywhere. This allows to create servers and to put them up in no time.

That is what we are going to use to create the server and play around with node.js and NginX.

Getting a droplet up and running is pretty quick, just go to digitalOcean sing up.

Having a droplet costs money but luckily if you have a .edu email linked to your github account you can get a $50 credit thanks to the GitHub Education Pack . If you don’t have a .edu email can get $10 here.

all this only works if you have never used digitalOcean.

Go get your free money (I’ll be here waiting)

Once you have your code, just go to the billing section on settings and look for the window where to add the code. You should be notified once added.

So… You have money to get a droplet up and running now. Creating a droplet is fairly simple, you go to the create tab, select droplets, and then you set it up.

We are going to be using the basic Ubuntu 16.04.3 x64 system and the $5 per month option. You can upgrade later on if you wish to do so. The rest of info is not relevant right now, so you can leave the default settings.

Congratulations you have your first VPS up and running.


Secure Shell is what we are going to be using to access our server, this allows us to access a computer on a secure way through a insecure network.

ssh is done through the command line; and to do that you need your server IP and the user name. If you have followed everything properly if you go to your email you should have received and email form digitalOcean giving you your access info.

It should look like this

Now that you have that information open terminal and do the following:

ssh root@111.222.333.444

After that you are gonna be asked for your password and then you will be asked to change it again. because you know. sending passwords via email is not the best of ideas.

new user

root is the the main user in a OS, so it is not a very good idea to be playing around with it, specially if you happen to be a novice like we are.

This means that the standard procedure is to create a new user every time you start a new system.

adduser username

Not all users are born equal so new ones do not have the same privileges as the root user. This means that in order to proceed we need to give sudo privileges to the new user so we can use it almost as the same way as the root user.

usermod -aG sudo username

now the new user has a better chance at changing the system, it is more powerful, as long as you use “sudo” before those commands that require the privileges.

If you want to make your system a bit more secure you should generate a ssh-key. check this tutorial for that.

now that you have the new user up and running, logout and ssh again with the new user.

ssh username@111.222.333.444

Node js

Node is a javasacript open source server framework that runs on the V8 engine, this allows to use javascript to create contents outside the browser.

It is mainly used for server-side programs and it has one of the biggest open source package libraries there are; V8 .

To install node we need the script to do so, this script is on a website that means we need to use curl. make sure that yo are in your home directory.

curl is a tool to transfer data from or to a server that uses the most used protocols in web servers.

curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh

Once we get this script we run it. This will prep the system so we can install node normally with apt-get.

apt-get is the package manager for linux.

sudo bash nodesource_setup.sh

Once you have prepped the system you can use a regular command to install node

sudo apt-get install nodejs

Now that node is installed we can proceed to build the first server but to make sure everything you download with npm works, is better to also get the build essential package.

sudo apt-get install build-essential


Everything is on place to start creating the server, first we need a place where to keep the server file, therefore we need a directory, to do that we use the command mkdir (make directory)

mkdir 1stServer

Now we can start adding the contents, like the server file, to do that, we have to enter the directory, that is done using the cd command.

Is good to know that you can autocomplete the prompt you are writing by using tab after a X amount of letters;

i.e. if you are writing” 1stServer.js” you can write 1 and then tab, that would complete the prompt due not being any other files starting with a 1 in the current directory. This might change if there’s a file called “1stFile.html”, if you just write 1, the prompt is going to give you all the files that start with 1. That means that If you write “1stF” then the autocomplete will be “1stFile.html”.

cd 1stServer

Now that you are inside the directory, you can create the server file. Using the touch command

touch 1stServer.js

To edit the file we just created we use nano

nano is one of multiple text editors inside the command line, is one of the easiest to get used to and works in a very similar way than regular text editor. Others might be emacs, and vim.

nano 1stServer.js

When using node most of the time you are using certain modules , you make use of these modules by calling them. you do that by creating a variable that calls a require for the module 'http' that has the functions to create a http server.

After that we create the server and tell it what to do. in this case we are just sending a string to be displayed when you connect.

In the last line we are setting the port where to set all this up.

the port is the location where the server listens for a client to come up when connecting, so if you connect to a different one, the server won’t provide anything, but if you go to the correct place, the server knows what you are looking for and gives it to you.

var http = require('http'); // we call the module

http.createServer(function(req, res){ // server is set up to request and respond
    res.writeHead(200, {'Content-type':'text/plan'}); // we set the type of response, to plain text 
    res.write('Sup bois!'); // message to be sent
    res.end( ); // we are done with the response
}).listen(7000); // port where to listen for requests

To exit nano , there is a list of commands right in the bottom of the editor, to leave we press ctrl+x , confirm or change the name and exit.

To try out if the file works, we run it using the command ‘ node and the name of the file. To check if it’s working go to ( _whatever.is.your.ip:7000 _ ) in your web browser.

node 1stServer.js

If everything worked out, you should see something like this:


Here we are doing the same as before, but this time is a bit more complex. this is just a basic website that allows you to navigate between links as you would in any static html page.

as before, we create a folder to contain the files.

mkdir 2ndServer

and we mode in.

cd 2ndServer

and use nano to create 2ndServer.js and add the code below. Remember to exit with ctrl + x and save while doing so.

var http = require('http');
var fs = require('fs');
var httpServer = http.createServer(requestHandler);
var url = require('url');


function requestHandler(req, res) {

 var parsedUrl = url.parse(req.url);
 console.log("The Request is: " + parsedUrl.pathname);

 fs.readFile(__dirname + parsedUrl.pathname,
 function (err, data) {
 if (err) {
 return res.end('Error loading ' + parsedUrl.pathname);

Once you have this new .js file, do nano again and create the index.html file to put your html code in.

		<title>woah, it actually works</title>
		<div style="width: 500; margin: auto;margin-top: 10%;text-align: center;font-size: 30;font-family: Arial;font-weight: 700;">
			Hola Caracola!

			<img src="https://media.giphy.com/media/vmv47p4zksWDC/source.gif" alt="">

now you are done, kinda...


Having a server is great and all, but if you log out and, try to access the page, you’ll see that there is no server running, and that is not a nice thing.

To fix this what you need is to have an app that keeps this scripts even if you are out of the server. Usually forever is used, but it not very helpful if you are trying to keep track of multiple scripts, so to make that easier we are going to be using pm2, which is quite good at tracking and keeping open node applications.

installing it is pretty straight forward.

sudo npm install -g pm2

from here, to make call it, you need to use pm2 at the beginning of every command, and then the specific action you what to do. in this case we use start and the name of the file. make sure you are in the folder that contains the file, if you fail to do so, it might cause you problems when running it.

pm2 start 2ndServer.js

Now you could close the session and the server would still be running.

There are other commands you should know in case you are going to be using pm2. the first one is to stop any script you have already running. This would stop the server but still keep it in the list, so you can start it again. be mindful of the fact that the name that you give to the file is the name you are going to be referencing when applying commands to it.

pm2 stop 2ndServer

Sometimes you don’t want to stop a process, you just need to restart it

pm2 restart 2ndServer

To see if everything is how you want it to be, you ask for a list.

pm2 list

The last one is just so you can check the info of the files added to pm2.

pm2 info 2ndServer

links that either, you should check out, or have been mentioned in this post

  • digitalOcean is nifty, you should check it out!
  • if you are in any way interested in web dev, node.js is crucial.
  • GitHub Education Pack gives you free money in a bunch of services.
  • if you don’t have a .edu email get $10 for digitalOcean here.
  • for a more secure way to ssh use this tutorial
  • learn about the V8 engine that powers node.js.
  • pm2 will make your life so much easier.


Please, tell me how good or bad this post was. And comment on anything that comes up in that beautiful brain of yours.