Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating configurable middleware


Configurable middleware refers to functions that can be customized, meaning there are variables that are not hardcoded and can be passed as parameters to those functions.

Some of the most widely used configurable components are the static and the session middleware. For the first one, we can configure the path for the static resources along with some more advanced features, while the session middleware can accept parameters such as secret, key, and other settings, as shown in the following code:

var session = require('express-session');
app.use(express.static('/public'));
app.use(session({
  secret: 'random chars',
  key: 'session_id',
  cookie: {
    secure: true
  }
}));

Closures to the rescue

As mentioned earlier in this chapter, the call to app.use() expects a function as a parameter in order to work properly. The router (this used to be the dispatcher from the Connect framework) invokes the middleware directly, because it is the only component that knows...