In this tutorial we will see how to make requests to a WHOIS server in Node.js without using the shell.
The solution is to create a socket to the remote host on port 43 (for example whois.nic.it
)
passing the domain name and ending the request with the sequence \r\n
.
'use strict';
const net = require('net');
const sendWhoisRequest = ({ host, domain }) => {
const client = new net.Socket();
const msg = domain + '\r\n';
return new Promise((resolve, reject) => {
client.connect(43, host, () => {
client.write(msg);
});
client.on('data', data => {
resolve(data.toString());
client.destroy();
});
client.on('error', err => {
reject(err);
});
});
};
This way we avoid using the whois
command from the shell.