はじめに
HonoでPOSTのエンドポイントを作成し、動作確認のためcurlを叩いたところ404 Not Foundが返ってきました。
問題
実行したコマンド
curl -X POST 'http://localhost:3000/api/todos' -H 'Content-Type: application/json' -d '{"todo": "study"}'
# 404 Not Found
元のコード
app.post("/api/todos", async (c) => {
try {
const body = await c.req.json();
console.log(body);
} catch {
return c.json({ error: "failed to create todo" });
}
});
解決方法
tryの中でreturnした。
app.post("/api/todos", async (c) => {
try {
const body = await c.req.json();
console.log(body);
return c.json({ body });
} catch {
return c.json({ error: "failed to create todo" });
}
});
おわりに
returnしないと404が返ってくるのを知りませんでした。何も返ってこないだけかと思っていました。
