Changes to Express core and middleware system

Express 4 no longer depends on Connect, and removes all built-in
middleware from its core, except for the express.static function. This means that
Express is now an independent routing and middleware web framework, and
Express versioning and releases are not affected by middleware updates.

Without built-in middleware, you must explicitly add all the
middleware that is required to run your app. Simply follow these steps:

Install the module: npm install --save <module-name>

In your app, require the module: require('module-name')

Use the module according to its documentation: app.use( ... )

The following table lists Express 3 middleware and their counterparts in Express 4.

The routing system

Apps now implicitly load routing middleware, so you no longer have to
worry about the order in which middleware is loaded with respect to
the router middleware.

The way you define routes is unchanged, but the routing system has two
new features to help organize your routes:

A new method, app.route(), to create chainable route handlers for a route path.

A new class, express.Router, to create modular mountable route handlers.

app.route() method

The new app.route() method enables you to create chainable route handlers
for a route path. Because the path is specified in a single location, creating modular routes is helpful, as is reducing redundancy and typos. For more
information about routes, see Router() documentation.

Here is an example of chained route handlers that are defined by using the app.route() function.

express.Router class

The other feature that helps to organize routes is a new class,
express.Router, that you can use to create modular mountable
route handlers. A Router instance is a complete middleware and
routing system; for this reason it is often referred to as a “mini-app”.

The following example creates a router as a module, loads middleware in
it, defines some routes, and mounts it on a path on the main app.

For example, create a router file named birds.js in the app directory,
with the following content:

The built-in Express middleware functions express.favicon,
express.logger, express.methodOverride,
express.session, express.bodyParser and
express.errorHandler are no longer available on the
express object. You must install their alternatives
manually and load them in the app.

You no longer need to load the app.router function.
It is not a valid Express 4 app object, so remove the
app.use(app.router); code.

Make sure that the middleware functions are loaded in the correct order - load errorHandler after loading the app routes.

Changes to the app generator

Example

Execute the following command to create an Express 4 app:

$ express app4

If you look at the contents of the app4/app.js file, you will notice
that all the middleware functions (except express.static) that are required for
the app are loaded as independent modules, and the router middleware
is no longer explicitly loaded in the app.

You will also notice that the app.js file is now a Node.js module, in contrast to the standalone app that was generated by the old generator.

After installing the dependencies, start the app by using the following command:

$ npm start

If you look at the npm start script in the package.json file,
you will notice that the actual command that starts the app is
node ./bin/www, which used to be node app.js
in Express 3.

Because the app.js file that was generated by the Express 4 generator
is now a Node.js module, it can no longer be started independently as an app
(unless you modify the code). The module must be loaded in a Node.js file
and started via the Node.js file. The Node.js file is ./bin/www
in this case.

Neither the bin directory nor the extensionless www
file is mandatory for creating an Express app or starting the app. They are
just suggestions made by the generator, so feel free to modify them to suit your
needs.

To get rid of the www directory and keep things the “Express 3 way”,
delete the line that says module.exports = app; at the end of the
app.js file, then paste the following code in its place:

You have now moved the functionality of ./bin/www back to
app.js. This change is not recommended, but the exercise helps you
to understand how the ./bin/www file works, and why the app.js file
no longer starts on its own.