Chapter 9. Advanced Express

 

This chapter covers

  • Implementing authentication
  • URL routing
  • Creating a REST API
  • Handling errors

In this chapter, you’ll learn a number of advanced Express techniques that will enable you to take more advantage of the framework’s functionality.

To demonstrate these techniques, you’ll create a simple application that allows people to register and post public messages that are displayed, in reverse chronological order, for visitors to see. This type of application is known as a “shoutbox” application. Figure 9.1 shows the front and user registration pages. Figure 9.2 shows the login and post pages.

Figure 9.1. The front and registration pages of the shoutbox application
Figure 9.2. The login and post pages of the shoutbox application

For this application, you’ll add logic to do the following:

  • Authenticate users
  • Implement validation and pagination
  • Provide a public representational state transfer (REST) API to send and receive messages

Let’s dive in by leveraging Express for user authentication.

9.1. Authenticating users

In this section you’ll start working on the shoutbox application by creating an authentication system for it from scratch. Within this section you’ll implement the following:

  • Logic to store and authenticate registered users
  • Registration functionality
  • Login functionality
  • Middleware to load user information, on request, for logged-in users

9.2. Advanced routing techniques

9.3. Creating a public REST API

9.4. Error handling

9.5. Summary