DEV Community

Cover image for Simple REST API with Deno

Simple REST API with Deno

Mohamed Amine Griche on May 14, 2020

After 2 years of its first release, Deno v1.0 is finally here. So, what is Deno anyway ? Deno is a simple, modern and secure runtime...
Collapse
 
ug02fast profile image
Arthur Zhuk • Edited

Thanks for the simple proof-of-concept. How are you liking Deno so far? Do you see yourself using it in the future over Node?

Don't forget the import in app.ts

import router from './routes.ts'
Collapse
 
am77 profile image
Mohamed Amine Griche • Edited

I like it, and I love the built in typescript support, do I see myself using it in the future over node? maybe in the future.
Thank you

Collapse
 
hagopj13 profile image
Hagop Jamkojian • Edited

Thanks for the article, it was really helpful.
Taking this as a starting point, I created a similar Deno project that stores the data in MongoDB.
Have a look at it and let me know what you think: github.com/hagopj13/deno-books-api

Collapse
 
jlomaka profile image
Jlomaka

any, really ? Why then do you use TS, it would be better to write to JS right away, since there is no minute to go in and read which interface to use in request and response. For those who are also confused by this, here are the interface to import for these parameters:

import {Request, Response} from "https://deno.land/x/oak/mod.ts";
Enter fullscreen mode Exit fullscreen mode

Typing was not invented to simply ignore it, it was created to exclude a very large number of bugs at the time of writing before writing tests.

Collapse
 
am77 profile image
Mohamed Amine Griche

I totally agree with you and I am sorry for the bad practices in the code.
This was not supposed to be full fledged, secure, scalable app, it is just a simple demonstration on how you can create a simple deno REST, as simple as that.

Collapse
 
lukethacoder profile image
Luke Secomb

Great article.

Might need to add those last few lines to the controller.ts file.

/* return the book if found and undefined if not */
const searchBookByIsbn = (isbn: string): ( IBook | undefined ) => books.filter(book => book.isbn === isbn )[0]

export { getBooks, getBook, addBook, updateBook, deleteBook }
Collapse
 
am77 profile image
Mohamed Amine Griche

Ah yes, Thank you.

Collapse
 
rudy750 profile image
Rudy Sarmiento

Hey,
you also left out:

const app = new Application()

app.use(router.routes())
app.use(router.allowedMethods())

console.log(`Listening on port ${PORT} ...`)
await app.listen(`${HOST}:${PORT}`)
Collapse
 
am77 profile image
Mohamed Amine Griche

Thank you.

Collapse
 
paulotumba profile image
PauloTumba

const getBook = ({ params, response }: { params: { isbn: string }; response: any }) => {
const book: IBook | undefined = searchBookByIsbn(params.isbn)
let indece:any;

indece=params.isbn
let index=indece-1

if (book) {

response.status = 200
response.body = books[index]

} else {
response.status = 404
response.body = { message: Book not found. }
}

}

i made some changes, on get by id it was returning only the first register

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Writing TS code is mandatory or optional in Deno?

Collapse
 
rudy750 profile image
Rudy Sarmiento
Collapse
 
peterstorm profile image
Peter Storm

I'm a TS noob, but a Haskell intermediate. Is it really not possible to give types to request and response? Is there a library for that?

Great guide though!

Collapse
 
kvraamkey profile image
Ramki A

Great article. @vincent Mancini
May i know which tool you're for API testing.
Thanks

Collapse
 
am77 profile image
Mohamed Amine Griche

I used "Servistate HTTP Editor & REST API Client" a chrome extension.

Collapse
 
parth_kharecha profile image
Parth Kharecha

Thank you Vincent Mancini for example
I have tested this example ane add new small changes
here is the git repository link: github.com/parthkharecha/denojs-ap...

Collapse
 
teraaret profile image
teraaret

There is error in getBook method:

response.body = books[0]

must be:

response.body = book