DENO: Build a Weather API with Deno

Yash Garudkar
May 27, 2020 · 2 min read
Image for post
Image for post
Article Banner

Deno is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on productivity.

Deno was designed by Ryan Dahl wanted to fix all the inherent problems in Node.js®.

The technologies used in the creation of Deno include:

  • Rust (In Node.js C++ is used to write its core)
  • Tokio (the event loop written in Rust)
  • TypeScript (Deno supports both TypeScript and JavaScript)
  • V8 (Google’s JavaScript runtime used in Google Chrome and Node.js)

That’s it for specs for now, lets start building our API.

Prerequisites: make sure you have Deno installed on your system.

The weather API is a basic application to learn how deno works. This is GitHub repo for the same.

The ‘{ Application }’ is the class that wraps serve function from the http package. It has two methods .use and .listen. The .use function is used to access the middleware whereas the .listen is used to allocate a port and start a server.

The const app = new Application() creates an instance of the application class allowing us to access its functions.

app.use(router.routes()) and app.use(router.allowedMethods()) allow routes written in route.ts file we will create and allow methods available in router. But this can be learnt later on.

Here we import getWeather and router class from deno.

getWeather uses OpenWeatherMap API to get location-based weather conditions. It returns a .json object when specified with two parameters which include ISO 3166 country codes for city name, state, and country code, divided by a comma.

The router.get(‘/’) is a plain body route as we don’t want a null response as we open our API server.

As opposed to router.post(‘/:city/:country’) which takes two parameters as mentioned earlier, city and country.

In the post method, we use simple if-else to make sure both our parameters include city and country names.

If the condition is met we respond with status 200 which means OK and in the response body, we return a success true and the JSON object returned by the getWeather method.

Lastly, we export our router to make sure weather.ts gets access to our written methods.

That is all the code we needed to write, now open terminal and write the run command as —

deno run --allow-net weather.ts

The command requires an addition of — allow-net which gives the deno command to access network permissions.

Now go to localhost:5000/cityname/countryname and get the weather at your desired location.

That’s it! You have successfully made an API using Deno.

Thank You!

REST API using Deno coming soon 🙋‍♂️

Weekly Webtips

Explore the world of web technologies through a series of tutorials

By Weekly Webtips

Get the latest news on the world of web technologies with a series of tutorial Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Yash Garudkar

Written by

Computer Engineer and Tech Enthusiast

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Yash Garudkar

Written by

Computer Engineer and Tech Enthusiast

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store