🔖
【NextJs + ElysiaJs】NexJs&ElysiaJs【#3 Elysia Sub Router】
【#3 Elysia Sub Router】
YouTube: https://youtu.be/WJXP2JNi7cM
今回はサブルーターの設定を行います。
server/routes/hello.ts
import { Elysia, status, t } from "elysia";
export const helloRouter = new Elysia({ prefix: "/hello" })
.get("/", () => {
const hello = "Hello Elysia";
if (!hello) {
throw new Response("Internal Error", { status: 500 });
// return status(500)
}
return {
hello,
};
})
.post("/", ({ body }) => body, {
body: t.Object({
name: t.String(),
}),
});
app/api/[[...slugs]]/route.ts
import { Elysia } from "elysia";
import { helloRouter } from "@/server/routes/hello";
export const app = new Elysia({ prefix: "/api" }).use(helloRouter);
export const GET = app.fetch;
export const POST = app.fetch;
export const PUT = app.fetch;
export const PATCH = app.fetch;
export const DELETE = app.fetch;
export const OPTIONS = app.fetch;
app/page.tsx
import { api } from "@/lib/eden";
export default async function Home() {
const data = await api.hello.get();
return <div>{data.data?.hello}</div>;
}
Discussion