为了账号安全,请及时绑定邮箱和手机立即绑定

nodejs配置域名,常用2种方法

标签:
Node.js

    今天给大家说两种nodejs配置域名的方法,具体如下:

1,使用dns.lookup()方法

当我们要查询域名www.baidu.com对应的ip的时候,可以通过dns.lookup()来打到目的。

const dns = require('dns');
 
 dns.lookup('www.baidu.com', (err, address, family) => {
     if(err) throw err;
     console.log('百度ip: ' + address);
 });

我们知道,同个域名可以对应不同的ip。那么,如何才能获取一个域名对应的多个ip呢,可以使用下面的方法:

const dns = require('dns');
 let options = {all: true};
 
 dns.lookup('www.baidu.com', options, (err, address, family) => {
     if (err) throw err;
     console.log('百度ip:' + address);
 }),

2,使用dns.resolve4()方法

其实,上文的方法也可以使用dns.resolve4()方法来实现:

const dns = require('dns');
 //获取ipv4地址 
 dns.resolve4('www.baidu.com', (err, address) => {
     if (err) throw err;
     console.log(JSON.stringify(address));
 })

如果需要获取iPv6的地址,则用dns.resolve6()方法:

const dns = require('dns');
 //获取ipv4地址 
 dns.resolve6('www.taobao.com', (err, address) => {
     if (err) throw err;
     console.log(JSON.stringify(address));
 }

    以上便是nodejs配置域名的全部分享,更多内容可关注慕课网其他相关文章~


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消