I\'m new to Node.js, so I\'m still wrapping my head around asynchronous functions and callbacks. My struggle now is how to return a response after reading data from a file in an
To access a variable in a different function, when there isn't a shared scope, pass it as an argument.
You could just pass res
and then access both query
and send
on the one variable within the function.
For the purposes of separation of concerns, you might be better off passing a callback instead.
Then do_search
only needs to know about performing a query and then running a function. That makes it more generic (and thus reusable).
searcher.do_search(res.query, function (data) {
res.send(...);
});
function do_search(query, callback) {
callback(...);
}