Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Server-side JavaScript

Prof. Cesare Pautasso
[email protected]

Dynamic Web

Where are we?

Web Architecture

Apps and Web Servers

Hello World on the Server

var http = require('http');
function onrequest(request, response) { response.writeHead(200); response.end("Hello World\n"); }
var server = http.createServer(onrequest); server.listen(8000);
console.log("Listening on");

Server Programming

  1. The server should always be ready for handling client requests
  2. Requests should be answered as quickly as possible
  3. One server should handle many types of requests coming from multiple clients at the same time

Web Server Architecture

Web Server in JavaScript

var http = require("http");
var url = require("url");
function onrequest(request, response) { 
var pathname = url.parse(request.url).pathname; if (typeof rh[pathname] === 'function') { rh[pathname](request, response); } else { response.writeHead(404); response.end(); }
} http.createServer(onrequest).listen(8888);

Routing Request Handlers

//Request Handlers
var root = function(request, response) { … }
var hello = function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"}); response.write("…"); //body response.end();
//Routing Table (Map URL -> Request Handler)
rh["/"] = root;
rh["/hello"] = hello;

Working with requests


Working with URLs

var url = require(‘url’);
var p_url = url.parse('/status?name=cp', true);
p_url = {
  href: '/status?name=cp',
  search: '?name=cp',
  query: { name: ‘cp' }, 
  pathname: '/status'

Working with responses

response.writeHead(statusCode, [msg], [headers]);

Explicit Headers

response.setHeader(key, value);

Implicit Headers

Concurrency - Threads

Concurrency - Events


  • Pro: No synchronization needed, there is only one thread that reads/write variables shared between different request handlers
  • Con: A slow request handler will block the entire server. All I/O operations need to be non-blocking


  • Con: Shared variables between thread need to be protected from concurrent access
  • Pro: Threads run in parallel, if one is slow/blocked, it will not delay all other pending requests (unless there is a shared lock)

Non-blocking callbacks

//blocking function
var y = f(x);

Blocking functions return their result when they finish

//non-blocking function
function(arguments, callback)
f(x, function(y) { ... });

Non-blocking functions return immediately and use callbacks to pass the results later when it's ready

Composing callbacks

var y = f(x);
var z = g(y);
var z = g(f(x));

Compose two non-blocking functions f and g and log the result in the console

Request Events

var postData = "";
  function(chunk) { postData += chunk; });
  function() { 
    console.log ("received" + postData); 

Read the body of a post request



Use a spacebar or arrow keys to navigate