Files
JS/02_advanced/uebungen/u13_dns-lookup/index.js
T
2026-09-24 14:40:26 +02:00

78 lines
1.7 KiB
JavaScript

// TODO: imports
const IP_V = 4; // we use IP protocol version 4
const URL = 'google.de';
// TODO: your modifications
// 1.
// const getIpBy = () => {};
// 2.
// Promises Unterobjekt von Node
// const getPromisesIpBy = () => {};
// 3.
// Promisify Helferfunktion
// const getPromisifyIpBy =
// 4.
// Async await
// const getAsyncIpBy =
// 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));
});
getAsyncIpBy(URL, IP_V)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(color.red(err));
});
/* Übung 13: Das Telefonbuch des Internets
Vielleicht hast du einmal die Webseite www.nodejs.com besucht? Dann hat dein Computer die so genannte IP-Adresse des Servers www.nodejs.com herausgefunden.
Ähnlich wie ein Telefonbuch ordnet das Domain Name System (DNS) jedem Server (Host) eine eindeutige IP-Adresse zu, unter der er über das Internet erreichbar ist.
Die Node.js-API bietet eine Funktion, um Namen im DNS nachzuschlagen: dns.lookup(…).
Die Übung besteht nun darin, obiges Beispiel so umzuschreiben, dass der Aufruf von dns.lookup(…) in eine Wrapper-Funkion verpackt wird.
Es sollen drei verschiedene Lösungen erstellt werden:
1. Ausführliche Variante mit eigenem Promise-Objekt als Ummantelung
2. Kurze Variante mit Promises-Unterobjekt (dns.promises)
3. Promisify-Variante mit util.promisify(…)
4. Async await Variante
*/