Skip to content

Chanzhaoyu/hono-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hono Snippets

This extension is a set of Snippets for Hono. They are created so that scaffolding with Hono can be easy. You can try to remember most of these.

Cover

Snippets Documentation

Prefix Description
h Hono Snippets

Snippets

h:base
import { serve } from "@hono/node-server";
import { Hono } from "hono";
const app = new Hono();

app.get("/", (c) => c.text("Hello Hono!"));

const port = 3000;
serve({
  fetch: app.fetch,
  port,
});
h:not-found
app.notFound((c) => {
  return c.text("Custom 404 Message", 404);
});
h:error-handling
app.onError((err, c) => {
  console.error(`${err}`);
  return c.text("Custom Error Message", 500);
});
h:api-get
app.get("/", (c) => {
  return c.text("GET /");
});
h:api-post
app.post("/", (c) => {
  return c.text("POST /");
});
h:api-put
app.put("/", (c) => {
  return c.text("PUT /");
});
h:api-delete
app.delete("/", (c) => {
  return c.text("DELETE /");
});
h:api-upload
app.post("/upload", async (c) => {
  const body = await c.req.parseBody();
  console.log(body["file"]);
});
h:param
const param = c.req.param();
h:query
const key = c.req.query("key");
h:body
const body = await c.req.parseBody();
h:json
const json = await c.req.json();
h:middleware
import type { Context, Next } from "hono";

export const middleware = async (c: Context, next: Next) => {
  await next();
};
h:serve-static
import { serveStatic } from "@hono/node-server/serve-static";

app.use("/*", serveStatic({ root: "./public" }));
h:cors
import { cors } from "hono/cors";

app.use(
  "*",
  cors({
    origin: "*",
    allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
  })
);
h:logger
import { logger } from "hono/logger";

app.use(logger());
h:compress
import { compress } from "hono/compress";

app.use(compress());
h:get
const value = c.get("key");
h:set
c.set("key", "value");
h:context-interface
declare module "hono" {
  interface ContextVariableMap {}
}

License

MIT