Monday, 19 March 2018

res.write spanning multiple functions?

I am trying to display domain searching results from a user-search. The flow of the application is:The user make a search (could be stackoverflow.com).The application returns a result from that search.In the background the application keeps searching for other domains and displays the results when they are ready.I am using Express in Node.js and from what I understand, it would be possible using res.write() and finish it with a res.end(). However, with my current setup I just get a "Write after end" error.I assume it's due to Javascript asynchronous nature, but I am a new developer, so that would just be my guess.This is my code pre-res.write. How would I rewrite it the best possible way, to create a res.write stream?router.get("/new", function(req, res) { //Strip all subdomains - the post var now only contains the domain and TLD. var post = getDomain(req.query.id); //Check if the user inserts a valid TLD. if (!tldExists(post)) { return res.send(JSON.stringify("invalidTld")); } //I want to replace every res.send with res.write after this comment functions.singleDomain(parse(req.query.id).hostname) .then(function(value) { res.send(JSON.stringify(value)); }) .catch(function(error) { res.send(JSON.stringify(error)); }) functions.checkDB(post) //Check if domain has been resolved within the last 24 hours. .then(function(value) { let promise; if(value !== false) { console.log("Domain has been resolved within the last 24 hours"); return res.send(JSON.stringify(value)); } else { promise = functions.lookupDomain(post); } return promise; }) .then(function(value) { res.send(JSON.stringify(value)); }) .catch(function(error) { console.log(error); }) });

Submitted March 19, 2018 at 04:35PM by HumbleBobcat

No comments:

Post a Comment