Opens an external site in a new window
Mental Health Awareness Month
“Community”
RODNEY LAB
  • Home
  • Plus +
  • Newsletter
  • Links
  • Profile
RODNEY LAB
  • Home
  • Plus +
  • Newsletter
  • Links

Deno Get Client IP Address: Remote User IP 🫱🏾‍🫲🏼 # Deno Get Client IP Address: Remote User IP 🫱🏾‍🫲🏼 #

blurry low resolution placeholder image Get Deno Client IP Address
  1. Home Rodney Lab Home
  2. Blog Posts Rodney Lab Blog Posts
  3. Deno Deno Blog Posts
<PREVIOUS POST
NEXT POST >
LATEST POST >>

Deno Get Client IP Address: Remote User IP 🫱🏾‍🫲🏼 #

Updated 13 months ago
3 minute read
Gunning Fog Index: 6.1
Content by Rodney
blurry low resolution placeholder image Author Image: Rodney from Rodney Lab
SHARE:

👋🏽 Deno Server Remote IP #

In this Deno get client IP address post, we have a quick look at legitimate reasons for needing the remote user IP address, before turning to three solutions. Those methods should be useful when you are running a Deno server and want to know the IP address of connecting clients; for spam filtering, as an example.

You see both how to access the client IP using Deno APIs and also using HTTP headers and when you might use each. I hope it will be useful for you. You can drop a comment below or reach out for a chat on Element , as well as Twitter @mention  if you have suggestions for improvements or questions.

📹 Video #

Please enable JavaScript to watch the video 📼

Get Deno Client IP Address: Remote User IP

🗳 Poll #

How do you filter out bot form submissions?
Voting reveals latest results.

🖥 Deno Get Client IP Address: Code #

main.ts
typescript
    
1 import { healthCheck } from "@/routes/health_check.ts";
2 import { logRequest } from "@/utilities/logging.ts";
3 import { load } from "@std/dotenv";
4
5 await load({ export: true });
6
7 const PORT = 8080;
8
9 function remoteAddress(
10 connectionInfo: Deno.ServeHandlerInfo
11 ): string | undefined {
12 const { remoteAddr } = connectionInfo;
13 if (!("hostname" in remoteAddr)) {
14 return undefined;
15 }
16
17 const { hostname } = remoteAddr;
18 return hostname;
19 }
20
21 const handler: Deno.ServeHandler = (request, connectionInfo) => {
22 const { method, url } = request;
23 const { pathname } = new URL(url);
24 logRequest(request);
25
26 switch (pathname) {
27 case "/health_check": {
28 if (method === "GET") {
29 const response = healthCheck();
30 return response;
31 }
32 const response = new Response("Method Not Allowed, {status: 405}");
33 return response;
34 }
35 case "/v0/ip-example": {
36 if (method === "GET") {
37 const { headers } = request;
38
39 const denoIP = remoteAddress(connectionInfo);
40 const xForwardedIP = headers.get("X-Forwarded-For") ?? undefined;
41 const cloudflareIP = headers.get("CF-Connecting-IP") ?? undefined;
42
43 console.log({ denoIP, xForwardedIP, cloudflareIP });
44
45 const finalAnswer = cloudflareIP ?? xForwardedIP ?? denoIP;
46
47 console.log({ finalAnswer });
48
49 const response = new Response("OK");
50 return response;
51 }
52
53 const response = new Response("Method Not Allowed, {status: 405}");
54 return response;
55 }
56 default: {
57 const response = new Response("Not Found", { status: 404 });
58 return response;
59 }
60 }
61 };
62
63 Deno.serve({ port: PORT }, handler);

🔗 Links #

  • Deno Fresh testing article
  • Cloudflare HTTP request headers docs 
  • GitHub repo with full code 
  • Element chat: #Rodney matrix chat 
  • Twitter handle: @askRodney 

🏁 Deno Get Client IP Address: Summary #

How can you access remote user IP address in Deno? #

The Deno HTTP server provides connection information metadata. You can access this meta from the parameters on the server handler function. The first parameter is the request, and the second, is the connInfo object. From connInfo, destructure remoteAddr.hostname. Typically, that hostname will be the connecting IP address as a string. This might not be the remote client IP address, though. For example, if the DNS for your Deno server is proxied through Cloudflare, the hostname will be a Cloudflare IP, instead. In this case, consider using the X-Forwarded-For HTTP header or a Cloudflare CF-Connecting-IP header.

How can you get remote client IP address on a Deno server behind a Cloudflare proxy? #

If your server is behind a Cloudflare (or other proxy), the Deno APIs will probably give you the proxy address, rather than the remote client IP address as the remote address hostname. As an alternative, look for the CF-Connecting-IP HTTP header on the incoming request.

What is the X-Forwarded-For HTTP header? #

When a client connects to your server via a proxy, the remote IP address, seen from your server, will appear to be the proxy IP address and not the remoter user’s IP. However, some proxies (Cloudflare, for example) will normally add the X-Forwarded-For HTTP header. The header value will be the remote user’s IP address; and this header makes the remove client IP address available to your server. When the request passes through multiple proxies, each will add their (adjacent) connecting IP to the header value. The header value can, therefore, be a comma separated list of IP addresses, which the request passed through. The remote client IP will be the first. Be sure to comply with appropriate privacy regulations when using the client IP address.

🙏🏽 Feedback #

Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.

blurry low resolution placeholder image ask Rodney X (formerly Twitter) avatar

Rodney

@askRodney

Just dropped a new video on how you can access remote client IP address form your 🦕 Deno server. Handy when running spam/bot detection.

Hope you find it useful!

#learndenohttps://t.co/Fak0uehRMI

— Rodney (@askRodney) July 14, 2023

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @[email protected]  on Mastodon and also the #rodney  Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as Deno. Also, subscribe to the newsletter to keep up-to-date with our latest projects.

Thanks for reading this post. I hope you found it valuable. Please get in touch with your feedback and suggestions for posts you would like to see. Read more about me …

blurry low resolution placeholder image Rodney from Rodney Lab
TAGS:
DENO
<PREVIOUS POST
NEXT POST >
LATEST POST >>

Leave a comment …

Your information will be handled in line with our Privacy Policy .

Ask for more

1 Nov 2022 — Astro Server-Side Rendering: Edge Search Site
3 Oct 2022 — Svelte eCommerce Site: SvelteKit Snipcart Storefront
1 Sept 2022 — Get Started with SvelteKit Headless WordPress

Copyright © 2020 – 2025 Rodney Johnson. All Rights Reserved. Please read important copyright and intellectual property information.

  • Home
  • Profile
  • Plus +
  • Newsletter
  • Contact
  • Links
  • Terms of Use
  • Privacy Policy
We use cookies  to enhance visitors’ experience. Please click the “Options” button to make your choice.  Learn more here.