Hello, everybody!
I have next block in Node.js:
app.post("/api/shorturl/new", function (req, res, next) {
dns.lookup(res.locals.url, function(err, adresses, family){
if (err){
res.locals.urlValid = false;
} else {
res.locals.urlValid = true;
}
});
next();
},
function (req, res) {
console.log("URL valid in next? ", res.locals);
if (res.locals.urlValid) {
res.json({origin_url: req.body.url});
}else {
res.json({error: "invalid URL"})
}
});
This code is not working, because app received response from dns.lookup() after app finish chain. So after if I include console.log() in dns.lookup(), it shows after last chain.
So my question is how to order app wait finish of dns.lookup() before handle next chain?