Implementing an Express middleware module

Let's return to the Node.js application we started in Chapter 2, Getting Started with Node.js. We're going to write an application where users can set puzzles for one another. First of all, we'll need a way of identifying the current user. We'll need to do this on most requests, making it a cross-cutting concern. This is a good use case for middleware.

For now, we will implement users in the simplest way possible, just storing an ID in a cookie. We will look into more robust identification in a later chapter. Note, however, that our use of middleware means it will be easy to alter our approach later on. This concern is encapsulated in our user middleware, so we only need to change it in one place.

First, ...

Get Learning Node.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.