Book Image

Learning Node.js for .NET Developers

Book Image

Learning Node.js for .NET Developers

Overview of this book

Node.js is an open source, cross-platform runtime environment that allows you to use JavaScript to develop server-side web applications. This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through understanding the Node.js programming model. You will learn how to build web applications and APIs in Node, discover packages in the Node.js ecosystem, test and deploy your Node.js code, and more. Finally, you will discover how to integrate Node.js and .NET code.
Table of Contents (21 chapters)
Learning Node.js for .NET Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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