foobar
foobar14mo ago

GetIP + Ctx from middleware to a route not passed

Hi, I have test to retrieve IP visitor Code taken from Discord is always returning "localhost" (when Deno Deploy) Also this value of "IP" get from the middleware is not passed to a route. What is wrong ? Repo is here : https://github.com/hapaxlife/fresh-show-ip _middleware
import { MiddlewareHandlerContext } from "$fresh/server.ts";

export interface State {
hostname: string;
}

function getIp(_req: Request, ctx: MiddlewareHandlerContext<State>) {
ctx.state = {}
if (ctx.remoteAddr.transport === "tcp") {
ctx.state.hostname = ctx.remoteAddr.hostname;
} else {
ctx.state.hostname = "not tcp";
}
ctx.state.data = "test data"
console.log("getIp : " + ctx.state.hostname)
return ctx.next();
}

export const handler = [
getIp,
];
import { MiddlewareHandlerContext } from "$fresh/server.ts";

export interface State {
hostname: string;
}

function getIp(_req: Request, ctx: MiddlewareHandlerContext<State>) {
ctx.state = {}
if (ctx.remoteAddr.transport === "tcp") {
ctx.state.hostname = ctx.remoteAddr.hostname;
} else {
ctx.state.hostname = "not tcp";
}
ctx.state.data = "test data"
console.log("getIp : " + ctx.state.hostname)
return ctx.next();
}

export const handler = [
getIp,
];
route : old way
import { Head } from "$fresh/runtime.ts";
import type { HandlerContext } from "$fresh/server.ts";
import { State } from "./_middleware.ts"

export default function Home(req: Request, ctx: HandlerContext<State>) {

console.log("Home " + ctx.state)
const hostname = ctx.state?.hostname || "no ip"

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
import { Head } from "$fresh/runtime.ts";
import type { HandlerContext } from "$fresh/server.ts";
import { State } from "./_middleware.ts"

export default function Home(req: Request, ctx: HandlerContext<State>) {

console.log("Home " + ctx.state)
const hostname = ctx.state?.hostname || "no ip"

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
Route : new way
import { Head } from "$fresh/runtime.ts";
import { State } from "./_middleware.ts"
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";

export const handler: Handlers = {
async GET(_req: Request, ctx: HandlerContext<State>) {
console.log("GET Handler")
console.log(ctx.state)
const resp = await ctx.render(ctx.state);
return resp;
},
};

export default function Page({ data }: PageProps<State>) {
const {hostname } = data;

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
import { Head } from "$fresh/runtime.ts";
import { State } from "./_middleware.ts"
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";

export const handler: Handlers = {
async GET(_req: Request, ctx: HandlerContext<State>) {
console.log("GET Handler")
console.log(ctx.state)
const resp = await ctx.render(ctx.state);
return resp;
},
};

export default function Page({ data }: PageProps<State>) {
const {hostname } = data;

return (
<>
<Head>
<title>fresh-show-ip</title>
</Head>
Visitor IP : {hostname}
</>
);
}
GitHub
GitHub - hapaxlife/fresh-show-ip: Show IP visitor
Show IP visitor. Contribute to hapaxlife/fresh-show-ip development by creating an account on GitHub.
0 Replies
No replies yetBe the first to reply to this messageJoin