The dns module provides DNS resolution and lookup capabilities. Here's how to use it for network operations.
Basic Lookup
Promise-based API
Lookup Options
Resolve DNS Records
SOA Records
SRV Records
Reverse DNS Lookup
Resolve Any Record Type
Custom DNS Servers
DNS Cache
Health Check with DNS
Error Handling
Best Practices
Lookup vs Resolve:
✓ lookup() uses OS resolver (cached)
✓ resolve() queries DNS directly
✓ Use lookup() for general use
✓ Use resolve() for specific records
Performance:
✓ Cache DNS results
✓ Use connection pooling
✓ Set reasonable timeouts
✓ Handle failures gracefully
Error Handling:
✓ Check error.code
✓ Implement retries
✓ Log DNS failures
✓ Have fallback servers
Avoid:
✗ Hardcoding IPs
✗ Ignoring TTLs
✗ Blocking on DNS
✗ No timeout handling
Conclusion
The Node.js dns module provides comprehensive DNS resolution capabilities. Use lookup() for general hostname resolution with OS caching, and resolve*() methods for specific record types. Implement caching for performance, handle errors with retry logic, and use custom resolvers for specific DNS servers. The promises API makes async DNS operations clean and composable for modern Node.js applications.