In this short blogpost we will take a look at Deno and write some small apps with it.
Introduction
Deno is a runtime for JavaScript , TypeScript which is based on V8 and Rust. It was co-created by Ryan Dahl who also created Node.js. Deno was announced at the JS Conf EU in 2018. In 2022 Deno got a lot of attention recently because since Deno 1.28 it introduced npm-support
Denos advantages over Node.js are TypeScript support out of the box and full compatibility with Web-APIs you can use just like in a normal browser.
To install Deno follow the instructions on the Deno Website once you done this you can write your first Deno program:
console.log("Hello Deno!")
Now you just need to run the deno run command in your terminal to run it:
deno run 01-hello-world.ts
Congratulations you wrote your first Deno programm.
basic webserver
Now we will write a basic webserver using Deno's standard library:
import { serve } from "https://deno.land/std@0.171.0/http/server.ts";
const port = 8080;
const handler = (request: Request): Response => {
const body = `Your user-agent is:\n\n${
request.headers.get("user-agent") ?? "Unknown"
}`;
return new Response(body, { status: 200 });
};
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);
await serve(handler, { port });
Note the import statement here we're importing a Deno module just with a weblink using ES modules rather than using Common JS as in Node.js.
You can try the webserver by running deno run --allow-net HTTP-Server.ts
note the --allow-net statement here, Deno code runs in a safe sandbox by default so you have to grant permissions for example get net access in your app.
You can now run http://localhost:8080/
and get your displayed which web browser you use.
npm support
Deno support Node.js npm modles which is handy since there not much Deno native modules this days the following example will import the Express framework:
import express from "npm:express@^4.17";
const app = express();
app.get("/", (req, res) => {
res.send("Hello World");
});
app.listen(3000);
console.log("listening on http://localhost:3000/");
This is the end of the first part a introduction to Deno comment below if you liked it or not based on the comments I will blog a second part soon stay tuned.
Top comments (0)