This commit is contained in:
54
02_advanced/uebungen/u13_dns-lookup/index.js
Normal file
54
02_advanced/uebungen/u13_dns-lookup/index.js
Normal 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));
|
||||
});
|
||||
28
02_advanced/uebungen/u13_dns-lookup/package-lock.json
generated
Normal file
28
02_advanced/uebungen/u13_dns-lookup/package-lock.json
generated
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
02_advanced/uebungen/u13_dns-lookup/package.json
Normal file
16
02_advanced/uebungen/u13_dns-lookup/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user