DEV Community

Cover image for Write your first API with Deno

Write your first API with Deno

Saswata Mukherjee on May 19, 2020

ICYMI, Deno v1.0 has been released! But what is Deno? Deno is a simple, modern and secure runtime for JavaScript and TypeScript that u...
Collapse
 
nikolabosnjak6 profile image
Nikola Bosnjak • Edited

Great post, thanks!

You dont have to expose db credentials, you can do it like this

just import --> import "deno.land/x/denv/mod.ts"; <-- to db.ts file

const dbName = Deno.env.get("DB_NAME") as string;
const dbHostUrl = Deno.env.get("DB_HOST_URL") as string;

Collapse
 
saswatamcode profile image
Saswata Mukherjee

Yup! Can do!

Collapse
 
micksp profile image
Michael Sperber

After the first code snippet, I get an error:

└─ $ ▶ deno run --allow-read --allow-net server.ts
error: Uncaught NotFound: No such file or directory (os error 2)
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendAsync ($deno$/ops/dispatch_json.ts:98:10)
    at async Object.open ($deno$/files.ts:37:15)
    at async Object.readFile ($deno$/read_file.ts:13:16)
    at async load (https://deno.land/x/denv/mod.ts:19:15)
    at async https://deno.land/x/denv/mod.ts:29:1

Any thoughts on whats going wrong?

deno 1.0.1
v8 8.4.300
typescript 3.9.2
Collapse
 
saswatamcode profile image
Saswata Mukherjee

Looks like your denv module is missing. Maybe you weren't connected to the internet when you ran the program and Deno couldn't load the module. You need an active net connection to get the denv and abc modules.

Collapse
 
micksp profile image
Michael Sperber

Thanks Saswata. I removed the cache dir (~/.cache/deno) and executed the deno run command again. Got the same error alas. This time I was on a network cable (previously on wifi) and deno didn't report any errors during all download. I'm giving up I'm afraid. The message is not very clear and Google is not my friend in this instance.

Collapse
 
nikolabosnjak6 profile image
Nikola Bosnjak • Edited

you are missing .env file

Collapse
 
micksp profile image
Michael Sperber

That was the solution! It seems we need an empty .env in the project dir. Thanks Nikola!

Thread Thread
 
nikolabosnjak6 profile image
Nikola Bosnjak

NP. I have made same mistake :D

Collapse
 
zhanyuzhang profile image
ChessZhang

Which deno version you used ? I use deno@1.0.0 to run your code will cause some problem.

Collapse
 
saswatamcode profile image
Saswata Mukherjee

For this article I have used:
deno 1.0.0
v8 8.4.300
typescript 3.9.2
If you have any further problems you can refer to the GitHub repo linked in the article!

Collapse
 
zhanyuzhang profile image
ChessZhang

Thanks. It works now!