创建帖子API
//src/index.ts
import { Elysia } from "elysia";
import {getDB} from "./utils/db";
//const app = new Elysia().get("/", () => "Hello Elysia").listen(3000);
const app = new Elysia();
const con = getDB()
//创建帖子
app.post("/posts/create-new-post",async (ctx) => {
//从请求体里面获取帖子字段
try {
const {title, content,author } = ctx.body
const [rows] = await con.execute('INSERT INTO `posts` (`title`,`content`,`author`) VALUE (?,?,?)',
[title,content,author]);
console.log('rows',rows)
if (rows.affectedRows === 0){
return new Response('发帖失败', { status: 500 });
}
return new Response('发帖成功',{status:200});
}catch (error:any) {
return new Response(error.message, { status: 500 });
}
})
app.listen(3000)