I am currently running a simple express server that includes a loop to check every domain in an array. The results of this process are being stored in an array called results
within each .then
statement of the promise. My question is, how can I send the results
in the server response after completing this promise block? Should I use a callback and call res.send(results)
once the promise is completed? Alternatively, is it possible to achieve this from within the promise using .finally
? Or should I utilize the express next
parameter instead? I am uncertain about the best approach.
const whois = require('whois-info');
const express = require('express');
const app = express();
app.get('/domainfinder/domain/:domainURL', (req, res) => {
//const domainURLs = req.params.domainURLs;
let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];
let results = [];
[...tests].forEach(domain => {
whois.lookup(domain)
.then(data => results.push(data))
.catch(e => console.log(domain, e.message))
});
res.send(results);
});
app.listen(3000, () => console.log('App listening on port 3000!'));