DEV Community

Cover image for API using Deno and ElyasiaJS
Santosh Anand
Santosh Anand

Posted on

API using Deno and ElyasiaJS

Elysia JS: TypeScript with End-to-End Type Safety, unified type system and outstanding developer experience. Supercharged by Bun.

Elasia is faster than Node, PHP and Gin as well

Image description

Here is simple code to create API

1. Install Deno: You can install Deno by following the instructions on its official website: https://deno.land/
2. Initialize a new Deno project:

mkdir my-api
cd my-api
touch server.ts
Enter fullscreen mode Exit fullscreen mode
deno install --allow-read --allow-write --allow-net --unstable -n elyasia https://deno.land/x/elyasia/cli.ts
Enter fullscreen mode Exit fullscreen mode

4. Create your API endpoint in server.ts:

import { App, Router } from "https://deno.land/x/elyasia@v0.8.0/mod.ts";

const app = new App();
const router = new Router();

// Define your API routes
router.get("/hello", (ctx) => {
  ctx.response.body = "Hello, World!";
});

app.use(router.routes());
app.use(router.allowedMethods());

// Start the server
app.listen({ port: 8000 });
console.log("Server is running on http://localhost:8000");

Enter fullscreen mode Exit fullscreen mode

5. Run your server:

deno run --allow-net --allow-read server.ts

Enter fullscreen mode Exit fullscreen mode

Now your API should be running on http://localhost:8000. You can test it by accessing http://localhost:8000/hello in your browser or using tools like curl or Postman.

This is a basic example to get you started. You can expand it by adding more routes, middleware, error handling, etc., based on your requirements.

for more information checkout https://elysiajs.com/

Top comments (0)