REST API: Exercise Solution
Let’s look at the solution for the REST API exercise.
We'll cover the following...
We'll cover the following...
Solution code
The code widget below contains the complete application to create a REST API server with Deno.
Remember: There are different ways to implement the same project, therefore it is absolutely fine if your solution has a different structure than the one shown below.
export interface Employee { id: string; firstname: string; lastname: string; email?: string; }
REST API exercise solution
Explanation
server.ts
-
Lines 13–18: We define our routes. When our server receives an HTTP request, it will check if the requested URL matches any of the routes we defined here. If there is a match, it invokes the callback passed as a second parameter.
-
Lines 20–22: We register different middleware for the routes and CORS with the
use()
methods.
data/data.ts
This class represents our data layer and simulates ...