Solution: Building an Express.js Server
Review the step-by-step solution to the exercise.
We'll cover the following...
We'll cover the following...
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:
Press + to interact
server.get('/api/hello', (req, res) => {res.send({ message: 'Hello from the API!' });});
Let’s break down the code for API routing: ...