express How does ExpressJs work Handling request/response


Example

The syntactic sugar

Most of the getting started examples of ExpressJs include this piece of code

var express = require('express');
var app = express();
...
app.listen(1337);

Well, app.listen is just a shortcut for:

var express = require('express');
var app = express();
var http = require('http');
http.createServer(app).listen(1337);

The Express App

The famous http.createServer accept a function which is known as the handler. The handler takes 2 parameters request and response as inputs, then manipulating them inside it's scope to do various things.

So basically app = express() is a function, taking place as the handler and dealing with request, response through a set of special components referred as middlewares.