Ben Dechrai
Ben Dechrai•5mo ago

Getting the client IP address with `deno serve`

I'm trying to get the IP address of the client when running declarative scripts through deno serve. It seems the fetch method only accepts the first request parameter, and if I expect and address the connInfo parameter, I get an Internal Server Error and a console output of TypeError: Cannot read properties of undefined (reading 'remoteAddr'). Are declarative scripts intended to not have access to this additional information? Or have I overlooked something?
export default {
async fetch(request, connInfo) {
const clientIP = connInfo.remoteAddr.hostname;
return new Response(`Hello world! Your IP address is ${clientIP}`);
},
};
export default {
async fetch(request, connInfo) {
const clientIP = connInfo.remoteAddr.hostname;
return new Response(`Hello world! Your IP address is ${clientIP}`);
},
};
Cheers, Ben
2 Replies
marvinh.
marvinh.•5mo ago
@Ben Dechrai The serve function only supplies one argument to the fetch function. This is because it's not yet specced and not compatible across runtimes. For that reason we went with the conservative route of only passing the first argument for now. It's worth filing an issue for it though, as this topic has flown under the radar as we're more focused on Deno 2
Ben Dechrai
Ben Dechrai•5mo ago
Thanks @marvinh. I'll send in an issue now 🚀