Fork me on GitHub

diet 0.9

Hosting

level:
duration:
15 minutes

Table of Contents

# Intro

The purpose of this document is to you give a general overview about the basics of backend development, the structure of the internet and how can you create web servers with Diet's Server Instances.

# Server Instances

In comparison to many other web frameworks diet has a structure that supports virtual hosting without any additional modules or configuration. Diet has Server Instances that function as virtual hosts.

Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name.


Server Instances are created by calling the server() function. They have a few methods listed in the Server API. You can set the hostname with the listen method as you can see on the examples below. You will see server instances defined as app's in the example snippets.

# Create HTTP Instances

The code below creates 2 Server Instances that listen on http://localhost:8000/ and another one on http://localhost:9000. We register a route on the "GET /" path for both instances and they respond with different messages.

// Require diet
var server = require('diet')

// Create Server Instance 1
var app = server()
app.listen('http://localhost:8000/')
app.get('/', function($){ 
    $.end('welcome to my website') 
})

// Create Server Instance 2
var app2 = server()
app2.listen('http://localhost:9000/')
app2.get('/', function($){ 
    $.end('welcome to my mobile api') 
})

curl "http://localhost:8000/"
# -> welcome to my website"
	
curl "http://localhost:9000/"
# -> welcome to my mobile api"

# Create HTTPS Instances

You can switch to HTTPS by changing http to https in the location attribute when you call the listen method. When you are using https as the protocol, you need to pass the source for the certificate files in the second argument of the listen method.

// Create Secure Server Instance
var app = server()
app.listen('https://localhost:9000/', {
   cert: 'cert.key',
   key: 'key.key'
})

# More about Backend Development

IP Address (Internet Protocol Address)

Every computer joins to the internet with an unique identifier, similar to government issued IDs. A computer's id is called an IP Address.

The Client and the Server

The client and the server are both computers that communicate with each other. The client is a computer that requests information from an another computer. The server is a computer that responds to an other computer that sends the request.



URL (Uniform Resource Locator)

The URL is used by a client computer to request information located on a different computer that we call a server. The URL contains informations that tells the server what is the client looking for on that server.

Hostnames & DNS (Domain Name System)

Hostnames have the same purpose as IP addresses but they are more human readable, easier to type and remember. Hostnames however need to be translated to IP addresses and that's why we need the Domain Name System.


Lets's see what happens when you type in http://google.com/ in your browser:


Internet

  1. Your computer contacts your ISP (Internet Service Provider)
  2. Your ISP uses the Domain Name System to find the IP behind google.com
  3. Your ISP contacts the Web Server behind http://195.13.231.178/ over the Internet
  4. The Web Server processes and responds to your request with a message
  5. The response message is sent back to the Internet, then to your ISP and finally back to your computer
  6. Your computer processes and displays the message from the server

# What's next?

Congrats! Now you know what are Virtual Hosts, how to create Server Instances and how your computer requested this page from my server to display it on your screen.


It's time to learn how to Route Pages within your web server.