Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

419
Views
Use la resolución de DNS personalizada para cualquier solicitud en NodeJS

Estoy buscando una manera de usar una resolución de DNS personalizada para una solicitud de nodejs usando node-fetch. Creo que hay una estrella para explicar aquí: resolución de IP de solicitud de anulación de nodo, pero no puedo hacer que funcione para ninguna solicitud. Mi objetivo es utilizar un solucionador de DNS alternativo, como cloudflare (1.1.1.1) o el DNS público de Google (8.8.8.8) en lugar de la resolución de DNS predeterminada del sistema operativo/ISP.

 import http from "http"; import https from "https"; import fetch from "node-fetch"; const staticLookup = (ip: string, v?: number) => (hostname: string, _: null, cb: Function) => { cb(null, ip, v || 4); }; const staticDnsAgent = (scheme: "http" | "https", ip: string) => { const httpModule = scheme === "http" ? http : https; return new httpModule.Agent({ lookup: staticLookup(ip), rejectUnauthorized: false }); }; // Both request are not working fetch(`https://api.github.com/search/issues?q=Hello%20World`, { agent: staticDnsAgent("https", "1.1.1.1") }) fetch(`http://jsonplaceholder.typicode.com/todos`, { agent: staticDnsAgent("http", "8.8.8.8") })

Estoy luchando un poco para encontrar una manera de hacer que este ejemplo funcione, estoy bastante seguro de que tengo que usar el módulo DNS de nodejs y configurar un servidor personalizado.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Gracias a Martheen que respondió en mi primera publicación, pude lograr el resultado aquí:

 import http from "http"; import https from "https"; import dns from "dns/promises"; import fetch from "node-fetch"; // Cloud flare dns dns.setServers([ "1.1.1.1", "[2606:4700:4700::1111]", ]); const staticLookup = () => async (hostname: string, _: null, cb: Function) => { const ips = await dns.resolve(hostname); if (ips.length === 0) { throw new Error(`Unable to resolve ${hostname}`); } cb(null, ips[0], 4); }; const staticDnsAgent = (scheme: "http" | "https") => { const httpModule = scheme === "http" ? http : https; return new httpModule.Agent({ lookup: staticLookup() }); }; fetch(`https://api.github.com/search/issues?q=Hello%20World`, { agent: staticDnsAgent("https") }) fetch(`http://jsonplaceholder.typicode.com/todos`, { agent: staticDnsAgent("http") })
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!