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, we need a way of generating unique IDs. For this, we will use the UUID module from npm. We can add this to our project by running the following on the command line:
> npm install uuid --save
The --save
flag stores the name of this module in our package...