REST API Endpoints

We will create the router and look at how to test our endpoints

REST API endpoints#

Now that we have confirmed our bare bones work, we can start adding the endpoints, but first we need to add some error handling and create some helpers.

Every API you will find in the wild is different and opinionated, so the way you will learn from me will of course be the same! There is no one correct way of doing things, and some ways will be better than others for particular use cases.

In the following sections, we will be adding a lot of code, so let's prep ourimport_map.json file before we begin.

Response structure#

Let's start with creating a unified response structure. One of the most important things when creating a serverwith multiple endpoints which will be consumed by a potential client, is to create standards. The most important standard to establish is the response structure.

We will be adhering to the web standards, and therefore also actively be usingHTTP status codes to tell the requester how the request went. An alternative is to always return200 - OK and be more verbose in the body, but I would personally not recommend this as it is easier to be web compliant.

Response body#

To be a bit more verbose, I always recommend to have a standard response body. This is my preferred format, which we will also use in this application:

But as creating this response for each endpoint can be a bit tiresome, we can add a helper function.

We will start by adding a couple of type helpers to main.ts since we will need it across our application, and now is a good time to add them.

  • AppState is the global state which will contain global accessible properties. We add this state to our contexts, and you will see in a later module why this is useful.

  • AppContext is the base Context type which we use with middlewares.

  • AppRouterContext is the Router Context which we will use in our models and controllers.

  • AppRouterMiddleware is the Router Middleware, it is used by our controllers.

Now, create a new folder src and add to it the file utils.ts.

We want to keep the root project folder as clean as possible which is why we place as much code as possible into src. The exceptions are main.ts because it is an entry point, and nessie.config.ts because it is a config file. The db folder should be kept separate from the rest of the code.

Add the following method:

We are passing in four arguments, two of them optional. The first argument is the app context which contains the request and response object, the second is for setting the status code, the third one is the data that we want to send back, and the fourth one is to be used in case of any errors.

In certain applications, the response can include files or other data, but in our case, we know that we will always only return JSON. We therefore set the default value to an empty object.


This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.