O'Reilly logo

Mastering Web Application Development with Express by Alexandru Vlăduțu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required