Deno Server

We will build our first server in Deno

Deno server#

One of the considerations for any module written in Deno is whether it is best to have one central dependency file, or if each file should only import the respectively needed modules. There are pros and cons with both ways, and I use either depending on the situation. Most libraries you come across will most likely have one deps.ts to keep versioning simple across the codebase.

Deno also supportsimport maps but these are more often used by applications, and you will most likely never see them being used in modules.

For the sake of this lesson, we will create a separate deps.ts file so that you can get comfortable with importing local files.

Create a new folder (you can name it after the title of this page) and add two files: deps.ts and main.ts. In the last step, we added the imports to ourmain.ts, but this time we will add the external imports into our deps.ts file.

Add the following imports to deps.ts:

Notice that we are using export instead of import to export the values to our other files.

Now it is time to create the server. Now that we have all our dependencies indeps.ts, we can import them from there. We start by adding green and serve to our imports in main.ts. Your main.ts file should now only have one line of code.


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

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