跳到正文

Oak

维护中
oakserver/oak

用于在 Deno、Node、Bun 和 Cloudflare Workers 上处理 HTTP 的中间件框架。

在 GitHub 查看
星标
5411
Fork
242
关注
5411
未关闭 Issue
52

该介绍目前仅提供英文版本。

Oak: Modern HTTP Middleware Framework

Oak is a powerful middleware framework for handling HTTP requests across multiple platforms. As a popular tool in the Deno ecosystem, it has garnered over 5,300 GitHub stars.

Core Features

  • Multi-Platform Support: Works seamlessly with Deno, Node.js, Bun, and Cloudflare Workers
  • Middleware Framework: Powerful middleware system for handling HTTP requests
  • Router Support: Built-in router for handling different routes and HTTP methods
  • Static File Serving: Easy serving of static files with ETag support
  • Error Handling: Comprehensive error handling middleware
  • TypeScript Support: Full TypeScript support out of the box
  • Modern API: Clean and intuitive API design

Common Use Cases

  • Building web applications and APIs
  • Creating middleware for request processing
  • Handling static file serving
  • Implementing RESTful services
  • Developing serverless applications
  • Building microservices

Quick Start

Basic example to create a simple web server:

import { Application } from "jsr:@oak/oak/application";

const app = new Application();

app.use((ctx) => {
  ctx.response.body = "Hello World!";
});

await app.listen({ port: 8000 });

Router Example

import { Application } from "jsr:@oak/oak/application";
import { Router } from "jsr:@oak/oak/router";

const router = new Router();
router
  .get("/", (ctx) => {
    ctx.response.body = "Hello World!";
  })
  .get("/users/:id", (ctx) => {
    ctx.response.body = `User: ${ctx.params.id}`;
  });

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });

Middleware Example

// Logger middleware
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.request.method} ${ctx.request.url} - ${ms}ms`);
});

// Error handling middleware
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.response.status = err.status || 500;
    ctx.response.body = {
      error: err.message
    };
  }
});

Deployment Options

Cloudflare Workers

import { Application } from "jsr:@oak/oak/application";

const app = new Application();

app.use((ctx) => {
  ctx.response.body = "Hello from Cloudflare Workers!";
});

export default {
  fetch: app.fetch,
};

Deno Deploy

import { Application } from "jsr:@oak/oak/application";

const app = new Application();

app.use((ctx) => {
  ctx.response.body = "Hello from Deno Deploy!";
});

await app.listen({ port: 8000 });

Documentation

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Special thanks to all contributors
  • Inspired by Koa
  • Built with ❤️ by the Oak team

按共同分类和标签排序的同类开源项目。

全部 Web 框架项目 →