Installing Express using NPM

We would like to use the Express framework which will make developing
our web server a lot more convenient. It abstracts difficult things and
provides lots of advanced functionality out of the box with a clear API.

To install express, and future packages, we use the node package manager
(npm). The npm executable checks if there is a package.json file in your
project and if you use the command "npm install" it will attempt to
install all the dependencies (recursively) that you gave in your
package.json file.

Your application can also be made available to others. If you decide to
do so later, other people will be able to add "hello-git-express" to
their list of dependencies. However, we will set this package to private
for the time being.

You can install the dependencies now using "npm install".
Ignoring node_modules using the .gitignore file

Including dependencies in your project is bad, mkay. It will make your
project unnecessarily bloated. Instead, we will require all users to run
"npm install" and we will run this any time. For *nix users this will
not be new. Package managers like apt, yum and pacman do the same thing
for packages on your system.

For Windows users this might be new, you can read more about it here:
TODO

Git can ignore files you mention in a special file called .gitignore.

We use this file to exclude the dir node_modules, which was created when
we ran "npm install".
Using Express in our code

So that concluded the setup part. Now that we are indeed set up
properly, we can start using Express.

We don't need the standard http server anymore, express will take care
of that. (It probably uses the http server inside, you can always just
check the source by going into your node_modules folder.)

Instead of requiring http, we now require express. The express server is
created in a different way, instead of exposing a createServer method,
the module itself can be called, which returns an instance of an express
server.

An express server starts listening the same way the standard http server
starts listening: we call listen(8080) on the object.

But how do we send responses? Well, we now limit request to /. If we try
to access /whatever, we will get an error.
Simplify, simplify, simplify

As I said earlier, Express makes things easier. In this case, it added a
send method the the res(ponse) which does what you usually want.

See http://expressjs.com/api.html#res.send for details.
Configuration done right

We would like to be able to configure things like compression,
internationalization, constants, port numbers, etc.

To do this, Express exposes the configure method, which in this form is
basically a direct callback. Why would we use this then? Well, it is
only ordering your configuration in one code block, which is not a
really good reason, but it is easier to read.

However, the configure method is not useless, it has another use which
allows you to configure development/testing/acceptance and production
environment in different ways. We shall see this later.

Right now we simply configure the port number.
Static resources

We need a way to serve static resources, like images. Express allows you
to do so by using middleware. The concept is a bit vague, I'll try to
explain.

In Express, each request passes through a list of middleware functions,
in predefined and strict order. Each middleware function gets the power
to do with this request what it wants:
 * it can process the request and return
 * it can modify the request and pass it on to the next middleware
 * it can ignore the request and pass it on to the next middleware

Right now, we want to register the built in "static file handler"
middleware. We register it, like all middleware functions, using the
use method.

We configure the static file handler in such a way that it tries to see
if the requested file exists on disk, under the directory 'static'. To
make this work, we create a directory static and place a file in there
called static.html .

If we now go to localhost:8080/ we still see 'Hello World'. If we go to
localhost:8080/static.html, we see 'Hello'.
Middleware order

The app.get('/', func..) statement is configuration for the Express
router. This is another piece of middleware.

If you now were wondering why the request to '/' still gave any results,
or why it ever gave any results at all, you score bonus points.

In a perfectly consistent world, we would have needed to register the
Express router middleware before something like the route registration
app.get('/', func..) would work.

It turns out that if the router was not registered, the router is
registered automagically when you register a route, like we did with
'/'.

So, in our previous example, the router was registered after we
registered the static middleware. This comes with a performance penalty,
as the static middleware (and thus, the disk) will now be hit for each
request. To fix this, we register the router explicitly before the
static middleware.