Open jasonleong opened 10 months ago
Here's an example of how to get the client IP
Deno.serve((req: Request, conn) => {
let ip = conn.remoteAddr.hostname;
return new Response("Hello World " + ip)
});
If you're using Hono server and want to inject the client IP into the env for downstream handling, here's a method that works;
const server = new Hono(options);
Deno.serve({
port,
handler: (req: Request, conn: Deno.ServeHandlerInfo) => server.fetch(req, { remoteAddr: conn.remoteAddr }),
});
// A Hono route handler in your code
type Bindings = {
remoteAddr: Deno.NetAddr;
};
app.get("/my-route", async (c: Context<{ Bindings: Bindings }>) => {
console.log(c.env.remoteAddr); // { transport: string, hostname: string, port: number }
...
});
file: /server/middleware/getip.ts
thank you!