This commit is contained in:
Philippe Torrel
2026-07-06 10:36:25 +02:00
parent 3578c15a45
commit 549233f42a
5 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
// const dns = require('node:dns'); // commonjs
import dns from 'node:dns'; // esm module
import util from 'node:util';
import color from 'chalk';
const IP_V = 4; // we use IP protocol version 4
const URL = 'google.de';
const getIpBy = (url, ipVersion = 4) => {
return new Promise((resolve, reject) => {
// async process
dns.lookup(url, ipVersion, (error, address) => {
if (error) {
reject(error);
} else {
resolve({ address: address, family: ipVersion });
}
});
});
};
// Promises Unterobjekt von Node
const getPromisesIpBy = (url, ipVersion = 4) => {
// async process
return dns.promises.lookup(url, ipVersion);
};
// Promisify Helferfunktion
const getPromisifyIpBy = util.promisify(dns.lookup);
// Test Cases:
getIpBy(URL, IP_V)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(color.red(err));
});
getPromisesIpBy(URL)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(color.red(err));
});
getPromisifyIpBy(URL, IP_V)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(color.red(err));
});

View File

@@ -0,0 +1,28 @@
{
"name": "u13_dns-lookup",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "u13_dns-lookup",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "u13_dns-lookup",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -78,6 +78,7 @@
case 'info': case 'info':
color = '#25cff2'; color = '#25cff2';
break; break;
case 'error':
case 'danger': case 'danger':
color = '#a52834'; color = '#a52834';
break; break;

View File

@@ -0,0 +1 @@
# Exkurs: Webseite mit Sass und package.json (scripts) Part II