Using Deno

In this lesson, we will look at general usage of Deno by running a file and also introduce the config file

This second part of the lesson will introduce you to some ways of using Deno. We will introduce the Deno config file, and also look at how to execute a file.

Executing a file with Deno#

In our directory, we will create a file called main.ts. If you are coming from JavaScript, there are two things you might be wondering about - why this file is called main and not index, and why the file has the .ts extension instead of .js.

The reasons are simple: Deno is built in Rust, which means that a lot of the conventions come from the better practices of Rust and not the (debatably worse) practices of Node. index has a lot of expectations in terms of how it works which is not applicable for Deno (you wont be able to access a file atdemo/index.js by importing demo).

We use ts because of Deno's native support for TypeScript as it parses the code to JavaScript and caches it before running the code.

Deno can handle js, ts, jsx, and tsx files as it parses TypeScript and runs JavaScript

However, you don't have to name your files according to any specific naming convention; after all, it's your program! You can call it whatever you like.Here is the Deno style guide if you want to learn more. Common names for entry files looks like follows:

FilenameDescription
main.tsUsed by applications to signify the main entry point and where to load the application from
mod.tsUsed by modules as an entry point to signify from where to load components
cli.tsUsed by modules where a command-line interface is available and serves as an entry point to the CLI
lib.tsNot as widely used as the prior three, but commonly used as an entry point for internally used components, e.g. used by cli.ts or mod.ts.

We now have an empty file called main.ts in our directory, and our next step will be to add some code.

Add a console.log to the file and save it.

Now is the time to run this file and try out the beauty of Deno for the first time!

 

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

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