[node.js] DNS module (resolve, JSON.stringify)

Hyo Kyun Lee·2021년 7월 26일
0

node.js

목록 보기
15/34

1. DNS module

해당 도메인 주소에 대한 err, address(IP주소)와 family(IP Version)을 반환한다.

"use strict";

const dns = require("dns");

//dns 문자주소를 탐색
dns.lookup("google.com", (err, address, family) => {
  console.log(`address :${address}, ${family}`);
});

dns.resolve4("google.com", (err, addresses) => {
  if (err) {
    console.error(err);
  } else {
    const res = JSON.stringify(addresses);

    console.log(res);

    addresses.forEach((a) => {
      dns.reverse(a, (err, hostnames) => {
        if (err) throw err;
        console.log(hostnames);
        console.log(`reverse for ${a}, ${JSON.stringify(hostnames)}`);
      });
    });
  }
  //reverse for IP address, www.google.com
});

결과화면


["216.58.197.238"]
address :142.250.196.110, 4
[ 'nrt13s49-in-f14.1e100.net', 'nrt13s49-in-f238.1e100.net' ]
reverse for 216.58.197.238, ["nrt13s49-in-f14.1e100.net","nrt13s49-in-f238.1e100.net"]

2. JSON.stringify

주어진 객체나 문자열의 값을 JSON표기법의 문자열로 변환하여 나타내준다.

보통 key : value, "key" : value의 형태로 표현하되, 문자열의 형태로 반환해주기 때문에 가독성을 높인 문자열을 출력받을 수 있다.

위 결과를 보면,
[ 'nrt13s49-in-f14.1e100.net', 'nrt13s49-in-f238.1e100.net' ] 의 배열이
["nrt13s49-in-f14.1e100.net","nrt13s49-in-f238.1e100.net"]
로 JSON.stringify (위 결과에서는 문자열 형태로만 변환되어 출력) 되었다.

3. 참조링크

dns module
http://www.w3big.com/ko/nodejs/nodejs-dns-module.html

JSON
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

0개의 댓글