Build secure applications with Deno's modern JavaScript runtime
# Deno Modern Runtime Development
Master modern server-side development with Deno using Google Antigravity IDE. This guide covers secure APIs, fresh framework, and deployment patterns.
## Why Deno?
Deno provides secure-by-default TypeScript runtime with modern APIs. Google Antigravity IDE's Gemini 3 engine suggests optimal Deno patterns.
## HTTP Server
```typescript
// main.ts
import { Application, Router } from "https://deno.land/x/oak@v12.6.1/mod.ts";
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
const app = new Application();
const router = new Router();
// Middleware
app.use(oakCors({ origin: "*" }));
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
console.log(`${ctx.request.method} ${ctx.request.url} - ${ms}ms`);
});
// Routes
router.get("/", (ctx) => {
ctx.response.body = { message: "Welcome to Deno API" };
});
router.get("/api/users", async (ctx) => {
const users = await getUsers();
ctx.response.body = { data: users };
});
router.get("/api/users/:id", async (ctx) => {
const { id } = ctx.params;
const user = await getUser(id);
if (!user) {
ctx.response.status = 404;
ctx.response.body = { error: "User not found" };
return;
}
ctx.response.body = { data: user };
});
router.post("/api/users", async (ctx) => {
const body = await ctx.request.body().value;
const user = await createUser(body);
ctx.response.status = 201;
ctx.response.body = { data: user };
});
app.use(router.routes());
app.use(router.allowedMethods());
// Error handling
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
console.error(err);
ctx.response.status = 500;
ctx.response.body = { error: "Internal server error" };
}
});
const port = parseInt(Deno.env.get("PORT") || "8000");
console.log(`Server running on http://localhost:${port}`);
await app.listen({ port });
```
## Database with Deno KV
```typescript
// db/kv.ts
const kv = await Deno.openKv();
interface User {
id: string;
email: string;
name: string;
createdAt: Date;
}
export async function createUser(data: Omit<User, "id" | "createdAt">): Promise<User> {
const id = crypto.randomUUID();
const user: User = {
id,
...data,
createdAt: new Date(),
};
const primaryKey = ["users", id];
const byEmailKey = ["users_by_email", data.email];
const result = await kv.atomic()
.check({ key: byEmailKey, versionstamp: null })
.set(primaryKey, user)
.set(byEmailKey, { id })
.commit();
if (!result.ok) {
throw new Error("Email already exists");
}
return user;
}
export async function getUser(id: string): Promise<User | null> {
const result = await kv.get<User>(["users", id]);
return result.value;
}
export async function getUsers(limit = 20): Promise<User[]> {
const users: User[] = [];
const iter = kv.list<User>({ prefix: ["users"] }, { limit });
for await (const entry of iter) {
users.push(entry.value);
}
return users;
}
export async function updateUser(id: string, data: Partial<User>): Promise<User | null> {
const existing = await kv.get<User>(["users", id]);
if (!existing.value) return null;
const updated = { ...existing.value, ...data };
await kv.set(["users", id], updated);
return updated;
}
export async function deleteUser(id: string): Promise<boolean> {
const existing = await kv.get<User>(["users", id]);
if (!existing.value) return false;
await kv.atomic()
.delete(["users", id])
.delete(["users_by_email", existing.value.email])
.commit();
return true;
}
```
## Fresh Framework
```typescript
// routes/index.tsx
import { Handlers, PageProps } from "$fresh/server.ts";
import { Head } from "$fresh/runtime.ts";
interface Data {
users: User[];
}
export const handler: Handlers<Data> = {
async GET(req, ctx) {
const users = await getUsers();
return ctx.render({ users });
},
};
export default function Home({ data }: PageProps<Data>) {
return (
<>
<Head>
<title>Antigravity Users</title>
</Head>
<div class="p-4 mx-auto max-w-screen-md">
<h1 class="text-3xl font-bold mb-4">Users</h1>
<ul class="space-y-2">
{data.users.map((user) => (
<li key={user.id} class="p-4 bg-gray-100 rounded">
<p class="font-semibold">{user.name}</p>
<p class="text-gray-600">{user.email}</p>
</li>
))}
</ul>
</div>
</>
);
}
```
## Testing
```typescript
// tests/user_test.ts
import { assertEquals, assertExists } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { createUser, getUser, deleteUser } from "../db/kv.ts";
Deno.test("User CRUD operations", async (t) => {
let userId: string;
await t.step("create user", async () => {
const user = await createUser({
email: "test@example.com",
name: "Test User",
});
assertExists(user.id);
assertEquals(user.email, "test@example.com");
userId = user.id;
});
await t.step("get user", async () => {
const user = await getUser(userId);
assertExists(user);
assertEquals(user?.name, "Test User");
});
await t.step("delete user", async () => {
const result = await deleteUser(userId);
assertEquals(result, true);
const user = await getUser(userId);
assertEquals(user, null);
});
});
```
## Deploy Configuration
```json
// deno.json
{
"tasks": {
"dev": "deno run --watch --allow-net --allow-env --allow-read main.ts",
"start": "deno run --allow-net --allow-env --allow-read main.ts",
"test": "deno test --allow-net --allow-env --allow-read",
"lint": "deno lint",
"fmt": "deno fmt"
},
"imports": {
"@/": "./src/",
"oak": "https://deno.land/x/oak@v12.6.1/mod.ts"
},
"compilerOptions": {
"strict": true
}
}
```
## Best Practices
- Use permissions flags for security
- Leverage Deno KV for simple storage
- Apply Fresh for server-rendered apps
- Import from deno.land/x for packages
- Use deno.json for project configuration
- Deploy with Deno Deploy for edge
Google Antigravity IDE provides Deno patterns and automatically suggests secure runtime configurations for your applications.This Deno prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their deno implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Deno projects, consider mentioning your framework version, coding style, and any specific libraries you're using.