Bun入门指南

获取某一篇帖子API

Preview
  • 获取某一篇帖子API

获取某一篇帖子API

//src/index.ts
//省略
app.get("/posts/:id",async (ctx) => {
    const { id } = ctx.params
    try {
        const [rows] = await con.query('SELECT * FROM `posts` WHERE `id`=?',[id]);
        if (rows.length === 0) {
            return new Response("没有找到相关帖子", { status: 404 });
        }

        return new Response(JSON.stringify(rows));
    }catch (error:any) {
        return new Response(error.message, { status: 500 });
    }
})
//省略

image.png