Deno 2.0 REST API Explained: Faster, Secure JavaScript Development

anticoder03

Ashish prajapati

Posted on October 15, 2024

Deno 2.0 REST API Explained: Faster, Secure JavaScript Development

Deno 2.0 is a major upgrade to the Deno runtime environment for JavaScript, TypeScript, and WebAssembly. It was created by the same developer who initially developed Node.js, and Deno aims to address some of the shortcomings found in Node.js, such as security, modularity, and built-in tooling.

Here are the key features of Deno 2.0:

  1. Node.js Compatibility: Deno 2.0 is fully compatible with Node.js, meaning that you can run Node.js code and use npm packages within the Deno runtime. This eliminates the need for separate runtime environments, allowing developers to leverage Deno's modern features without abandoning the rich ecosystem of npm.

  2. Performance Enhancements: Deno 2.0 has improved startup times and optimized memory usage. It makes use of the V8 engine for JavaScript execution, with faster HTTP processing and more efficient handling of common tasks. This improvement makes Deno more suitable for performance-sensitive applications.

  3. Security: One of the most significant features of Deno is its security model. By default, Deno's runtime does not allow access to the file system, environment variables, or network without explicit permissions. This is a major improvement over Node.js, which has no such restrictions.

  4. Native TypeScript Support: Unlike Node.js, which requires additional tools like Babel or ts-node to compile TypeScript, Deno natively supports TypeScript out of the box. You can run TypeScript code without any configuration, making the development process smoother for TypeScript developers.

  5. Simplified Package Management: Deno eliminates the need for a package.json file and node_modules folder. Instead, it uses URL-based imports, making dependency management more straightforward and reducing clutter in the project folder.

  6. Built-in Tooling: Deno comes with integrated tools such as a linter (deno lint), formatter (deno fmt), test runner, and bundler. This reduces the need for external packages or tools that are typically required in a Node.js project.

  7. Edge Deployments: Deno 2.0 also supports edge computing via Deno Deploy, which enables developers to deploy their applications closer to users, resulting in reduced latency and better performance for global applications.

Basic REST API Using Deno 2.0

Here’s a basic example of creating a REST API using Deno:

1. Setting up Deno

Make sure you have Deno installed by running:

deno --version
Enter fullscreen mode Exit fullscreen mode

If not installed, follow the instructions on Deno's official website.

2. Creating the REST API

We will use Deno's built-in HTTP server to create a basic REST API that handles GET, POST, PUT, and DELETE requests.

First, create a file named server.ts:

// Importing the serve function from Deno's standard library
import { serve } from "https://deno.land/std@0.196.0/http/server.ts";

// Function to handle requests
async function handler(req: Request): Promise<Response> {
  const { pathname, searchParams } = new URL(req.url);

  // Handling different routes
  if (pathname === "/api/greet" && req.method === "GET") {
    const name = searchParams.get("name") || "World";
    return new Response(`Hello, ${name}!`);
  }

  if (pathname === "/api/data" && req.method === "POST") {
    const body = await req.json();
    return new Response(JSON.stringify({ message: "Data received", data: body }), {
      headers: { "Content-Type": "application/json" },
    });
  }

  if (pathname === "/api/data" && req.method === "PUT") {
    const body = await req.json();
    return new Response(JSON.stringify({ message: "Data updated", data: body }), {
      headers: { "Content-Type": "application/json" },
    });
  }

  if (pathname === "/api/data" && req.method === "DELETE") {
    return new Response(JSON.stringify({ message: "Data deleted" }), {
      headers: { "Content-Type": "application/json" },
    });
  }

  // Default response for unknown routes
  return new Response("Not Found", { status: 404 });
}

// Start the HTTP server on port 8000
serve(handler, { port: 8000 });

console.log("Server running on http://localhost:8000");
Enter fullscreen mode Exit fullscreen mode

3. Running the API

To run the server, use the following command:

deno run --allow-net server.ts
Enter fullscreen mode Exit fullscreen mode

The --allow-net flag is required because Deno's security model restricts network access by default, and you need to explicitly allow it.

4. Testing the API

  • GET Request: Access http://localhost:8000/api/greet?name=Deno in your browser or via curl:
  curl http://localhost:8000/api/greet?name=Deno
Enter fullscreen mode Exit fullscreen mode

Response: Hello, Deno!

  • POST Request: Use curl to send a POST request with JSON data:
  curl -X POST http://localhost:8000/api/data -H "Content-Type: application/json" -d '{"id": 1, "name": "Deno"}'
Enter fullscreen mode Exit fullscreen mode

Response: {"message":"Data received","data":{"id":1,"name":"Deno"}}

  • PUT Request: Send a PUT request to update data:
  curl -X PUT http://localhost:8000/api/data -H "Content-Type: application/json" -d '{"id": 1, "name": "Updated Deno"}'
Enter fullscreen mode Exit fullscreen mode

Response: {"message":"Data updated","data":{"id":1,"name":"Updated Deno"}}

  • DELETE Request: Send a DELETE request to delete data:
  curl -X DELETE http://localhost:8000/api/data
Enter fullscreen mode Exit fullscreen mode

Response: {"message":"Data deleted"}

Conclusion

Deno 2.0 simplifies JavaScript and TypeScript development by focusing on performance, security, and modern developer needs like TypeScript support and edge deployment. This basic REST API example demonstrates how to get started with Deno’s built-in HTTP server, showcasing its simplicity and efficiency in handling common development tasks.

💖 💪 🙅 🚩
anticoder03
Ashish prajapati

Posted on October 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related