Building a restful API using Deno (Part1) 
Tags: deno, javascript, vscode, ubuntu
Published: 2020-05-31 16:54:34

Hey, dear mates!

How is it going? I hope you all are fine. :)

So, one week has already passed and it is time to put hands-on in the first part our project, right? Yeahhhh!

If you have no clue what I am talking about, please read my last two posts [Deno is not here to replace Node](https://dev.to/vinicius77/deno-is-not-here-to-replace-node-nmd), [Building a restful API using Deno (Intro)](https://dev.to/vinicius77/building-a-restful-api-using-deno-intro-1o96) and [Building a restful API using Deno (Part2)](https://dev.to/vinicius77/building-a-restful-api-using-deno-part2-5eo2)


I assume you guys have already some Javascript experience but feel free to point out if you didn't understand any code you saw, (through comments or DM me if you are shy), also if you know a best approach or even correct me if I made a mistake. 
All questions here worth gold and also helps me improving my way to learn, teach and create better content.

_"Anyone who has never made a mistake has never tried anything new."_ (Albert Einstein)

We are trying something brand new (**DENO**) so let's make mistakes together.

So, take your coffee mug, put The Sims build mode as your background music and _"letis getty ztared"_.{% youtube rAhwkPDYnUA %} 

### [Installation](https://github.com/denoland/deno_install)
I am going to be as succinct as possible in order to avoid being overwhelming so we can focus in the important parts that really need to be explained.
Said that, __let's kill two birds with just one bullet__ (**NOT** literally saying).

Open your [VSCODE](https://code.visualstudio.com/), click Terminal > New Terminal and type:

```
curl -fsSL https://deno.land/x/install/install.sh | sh
```
Now we have Deno installed.
*OPTIONAL: You can add the following VSCODE extension for better experience. [Visual Studio | Marketplace](https://marketplace.visualstudio.com/items?itemName=axetroy.vscode-deno).

Just that? Yes. 
(*HINT:* When following tutorials ALWAYS research what you are typing / copying and pasting in your terminal and from which source it came from.)

### Project Structure Folder
We could simply add all the code in just one file but it is a good practice to separate each module and their own folder/ file even our project is small.

I though in a structure similar to this:

![Alt Project Structure on VSCode](https://dev-to-uploads.s3.amazonaws.com/i/44rijadthyyifqfdap60.png)

### _config.js_
Create a config folder with a _config.js_ file inside of it. 

```javascript
/* I separated this small chunk of code in its own file because
   we will implement environment variables soon using Deno.env.toObject() */

export const PORT = env.PORT || 5000;
export const HOST = env.HOST || "127.0.0.1";
```
We simply have just assigned both values of _PORT_ and _HOST_ in order to use them to start our server.

NOTE: When we have sensible data that must not be exposed, for instance, a MongoDB connection string, we could use as an alternative https://deno.land/x/dotenv, a .env file and *_"git ignoring"_* it in your repository. By the way, it should be nice add this feature in the future but if you are in a fast pace than us just check the [deno-dotenv documentation](https://github.com/pietvanzoen/deno-dotenv/blob/master/README.md)

### _app.js_ file
```javascript

/* Here we are importing the Deno third part modules
   You can also copy and paste this URL in your browser to check the 
   documentation :) */ 
import { Application } from "https://deno.land/x/oak/mod.ts";

/* This line creates the new Application*/
const app = new Application();

export default app;
```
I know, there are actually more comments in this file than Javascript code. Please, be patient.

### _server.js_ file
```javascript
/* We are importing the values from the config file we created previously*/
import { PORT, HOST } from "./config/config.js";

/* The same with happening with this file line*/
import app from "./app.js";

/* console.log() our best friend */
console.log(`Listening on ${HOST}:${PORT}`);

/* Our application will listen in the host and port we assign*/
await app.listen(`${HOST}:${PORT}`);

```
Did you notice something weird in this file other than my comments? No?
Read it again carefully. Not yet?
Wait, we are using *_await_* without *_async_* and no, it was not my mistake. Different of Node, Deno is *_async_* by default, in other words, when in global scope you don't have to create an *_async_* wrapper function just to use *_await_*. That is one of the magical features that Deno brings for us.

Let's run our tiny but not least application. I assume that you are in your project directory's folder.

```
deno run server.js
```
We got the error below but I will explain why.
```
error: Uncaught PermissionDenied: access to environment variables, run again with the --allow-env flag
at file:/home/kako77sub/deno_projects/deno-restful-improved/config/config.js:1:22

```
Deno entirely controls what programs can access / run or not. The *security* is the main reason why Deno exists and it is why we should draw our attentions on it. Let's run it again but this time adding the _--allow-env_ flag.

```
deno run --allow-env server.js
```
We got another error. It happened because our app.js file imports content from external resouces (e.g. https://deno.land/x/oak/mod.ts). _Easy peasy lemon squeezy_, the error message already gave us a hint.

```
deno run --allow-env --allow-net server.js
```
If I did everything how it supposed to be done you should see in your terminal something like:
```
Listening on 127.0.0.1:5000
```

### We got it!

We already have our server running (To stop it the old _CTRL + C_ should be enough). 
So what does it do? Honestly, nothing yet because we didn't implement the routers and controllers.

It is going to be our next task in the next post. 

Thanks and see you all on next weekend.