Solution: Building an Express.js Server

Review the step-by-step solution to the exercise.

In this exercise, your objective was to complete the following task:

  • Create an API route named /api/hello that returns a JSON response.

  • Create a middleware to handle server-side errors and respond with a 500 status code and an error message.

  • Implement a middleware to handle 404 errors and respond with a 404 status code and a “Page Not Found” message.

In the next few sections, we’ll review the solution for each task individually.

API routing

Here’s the code for the solution:

Get hands-on with 1200+ tech skills courses.