Deno CRUD REST API Example

1+

Deno CRUD REST API

Today, we are going to create a Deno CRUD REST API with simple example. Recently, I covered the tutorial on VDOM (Vue, Deno, Oak, and MongodB) stack application. Deno is the infant secure runtime for JavaScript and TypeScript. Many of the reader asked me to write an article to create rest api using Deno.

Deno CRUD REST API Example
Deno CRUD REST API Example

Generally, if you are familiar with Node.js then Deno is just like that. Except that it’s improved in many ways, it’s created from bottom to top to be a better implementation of Node.js.

Why I use Deno over Node? ?

Since Deno and Node.js serve the same purpose, it’s possible to compare the two directly.

Node Deno
Engine V8 V8
Written in C++ & JavaScript Rust & Typescript
Package managing package managers: npm uses URLs
Importing pacakges CommonJS syntax ES Modules
Security full access permissioned access
TypeScript support not built in built in

Okay, cool!! So How can I install Deno in my system? ?

Install Latest Version

With Shell:


With PowerShell:


For more details check Deno’s official installation guide.

After install check the version of Deno using deno –version

deno --version
deno –version

Now you can run deno repl to open the REPL (Read-Execute-Print-Loop) and start typing some JavaScript.

deno repl
deno repl

Let’s run some Deno example

The entry point can be .js file, .ts file or even a URL that points to an app entry point and Deno will download, compile and then run it for you:

For example: deno run https://deno.land/std/examples/welcome.ts

Deno CRUD REST API, Deno CRUD REST API Example

Come out from playground and Build an API ?

Just like in our previous example, we’ll use Oak framework and Typescript (you can also use JavaScript if you want to).

First of all, create three files app.ts, router.ts and controller.ts

In the app.ts  file, we have to imports the Application from https://deno.land/x/oak/mod.ts and the router from ‘./router.ts‘.

Then set the PORT and HOST


After that, add the code of router.ts. In this file, we have to import Router from https://deno.land/x/oak/mod.ts and create the routes.


At line number 2 in above code, the functions getBooks, getBook, addBook, updateBook and deleteBook are imported from a local file, it’s just we haven’t created them yet.

Before creating these functions in controller, let me finish our app.ts file firstly.


Now it’s time to create controller.ts. In this file, lets define an interface for a book IBook, then we declare an initial array of book objects:

Where is the implemented functions in controller.ts?

Now we’ll implement the functions which we define in our router.ts.

Function Route Description
getBooks GET /books return all the books
getBooks GET /books/{isbn} return a book
addBook POST /books add a book
updateBook PUT /books/{isbn} update a book
deleteBook DELETE /books/{isbn} delete a book

getBooks: returns all the books on the list.


returns all the books on the list.
returns all the books on the list.

getBook: returns a single book by its isbn, or error message if not found.


Returns a single book by its isbn, or error message if not found
Returns a single book by its isbn, or error message if not found

addBook: add a book to the books list.


Add Book
Add Book

After book add
After book add

updateBook: updates a book if exists, return error message if not.


Update Books
Update Books

After Update Books
After Update Books

deleteBook: deletes a book from the books list.


Delete Books
Delete Books

After Book deletion
After Book deletion

Here we finish our controller.ts. Full code would be:

Why searchBookByIsbn is used in controller.ts?

This is a helper function to find the book by ISBN id and we call this function at line number 26.

Run the Deno code now

Download source code from GitHub

If you like this article then please give your heart and share with your friends.

 

1+
Summary
Deno CRUD REST API Example
Article Name
Deno CRUD REST API Example
Description
Today, we are going to create a Deno CRUD REST API with simple example. Recently, I covered the tutorial on VDOM (Vue, Deno, Oak, and MongodB) stack.
Author
Publisher Name
Das JS
Publisher Logo