Elysia 是一个轻量级且高性能的 TypeScript 后端框架。使用 Bun 作为运行时环境可以提升 TypeScript 项目的性能和开发效率。下面是一个在 Bun 环境下,使用 Elysia 创建的复杂实例。
安装 Bun 和 Elysia
首先,确保你已经安装了 Bun。可以按照官方网站上的说明进行安装:
curl -fsSL https://bun.sh/install | bash然后,创建一个新的项目并安装 Elysia:
mkdir elysia-app
cd elysia-app
bun init
bun add elysia项目结构
创建以下项目结构:
elysia-app/
├── src/
│ ├── index.ts
│ ├── routes/
│ │ ├── users.ts
│ │ ├── posts.ts
│ └── models/
│ ├── user.ts
│ └── post.ts
├── package.json
└── tsconfig.json
配置 tsconfig.json
确保 tsconfig.json 包含以下配置:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": ["src"]
}定义模型
src/models/user.ts
export interface User {
id: number;
name: string;
email: string;
}src/models/post.ts
export interface Post {
id: number;
title: string;
content: string;
authorId: number;
}定义路由
src/routes/users.ts
import { Router } from "elysia";
import { User } from "../models/user";
const users: User[] = [
{ id: 1, name: "Alice", email: "alice@example.com" },
{ id: 2, name: "Bob", email: "bob@example.com" }
];
export const userRouter = new Router()
.get("/", (req, res) => {
res.json(users);
})
.get("/:id", (req, res) => {
const user = users.find((u) => u.id === parseInt(req.params.id));
if (user) {
res.json(user);
} else {
res.status(404).send("User not found");
}
})
.post("/", (req, res) => {
const newUser: User = req.body;
newUser.id = users.length + 1;
users.push(newUser);
res.status(201).json(newUser);
});src/routes/posts.ts
import { Router } from "elysia";
import { Post } from "../models/post";
const posts: Post[] = [
{ id: 1, title: "First Post", content: "This is the first post", authorId: 1 },
{ id: 2, title: "Second Post", content: "This is the second post", authorId: 2 }
];
export const postRouter = new Router()
.get("/", (req, res) => {
res.json(posts);
})
.get("/:id", (req, res) => {
const post = posts.find((p) => p.id === parseInt(req.params.id));
if (post) {
res.json(post);
} else {
res.status(404).send("Post not found");
}
})
.post("/", (req, res) => {
const newPost: Post = req.body;
newPost.id = posts.length + 1;
posts.push(newPost);
res.status(201).json(newPost);
});主文件
src/index.ts
import { Elysia } from "elysia";
import { userRouter } from "./routes/users";
import { postRouter } from "./routes/posts";
const app = new Elysia()
.use("/users", userRouter)
.use("/posts", postRouter);
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});运行项目
使用以下命令运行项目:
bun run src/index.ts访问 API
现在,你可以通过以下 URL 访问 API:
- 获取所有用户:
GET http://localhost:3000/users - 获取单个用户:
GET http://localhost:3000/users/:id - 创建新用户:
POST http://localhost:3000/users(需要提供用户数据) - 获取所有帖子:
GET http://localhost:3000/posts - 获取单个帖子:
GET http://localhost:3000/posts/:id - 创建新帖子:
POST http://localhost:3000/posts(需要提供帖子数据)
总结
这个示例展示了如何在 Bun 环境下使用 Elysia 创建一个简单的后端应用。通过定义模型、路由和主文件,可以构建一个结构化的项目,支持用户和帖子管理的基本 CRUD 操作。Elysia 的轻量级特性和 Bun 的高性能使得这个组合非常适合现代 TypeScript 开发。