Note that there are some explanatory texts on larger screens.

plurals
  1. POinjecting arguments versus returning data in node.js
    primarykey
    data
    text
    <p>I'm trying to understand when to inject something like the response into a function, versus calling a function and returning something from it. In Node.js that is.</p> <p>Do any functions in Node.js return data? Or is it all about injecting arguments and using callbacks?</p> <pre><code>var sendHTMLToBrowser = require("../myCode/andrewsHelpers").sendHTMLToBrowser; //var execExternalCommand = require("./childProcesses").execExternalCommand; // import the exec function defined on the child_process module var exec = require('child_process').exec; function start(response, request) { console.log("Request handler 'start' was called"); var body = '&lt;!doctype html&gt;'+ '&lt;html lang="en"&gt;'+ '&lt;head&gt;'+ '&lt;meta charset=UTF-8" /&gt;'+ '&lt;/head&gt;'+ '&lt;body&gt;'+ '&lt;p&gt;Hello Andrew :)&lt;/p&gt;'+ '&lt;/body&gt;'+ '&lt;/html&gt;'; sendHTMLToBrowser(response, body); } function linecount(response, request) { console.log("Request handler 'linecount' was called."); // launch the command "cat *.js | wc -l" exec('cat *.js | wc -l', function(err, stdout, stderr) { // the command exited or the launching failed if (err) { // we had an error launching the process console.log('child process exited with error code', err.code); return; } sendHTMLToBrowser(response, stdout.toString()); }); } function executeCommand(command) { return result; } exports.start = start; exports.linecount = linecount; </code></pre> <p>So I've hardcoded in some code to display the linecount in the browser. What if I wanted to make a more generic function, that took a command as a string and returned the output? Would I again inject response into this function or could I return the result?</p> <p>Is it a question as to whether a command will potentially become a block? e.g. doing some basic string manipulation could probably be implemented as a function that returns a value?</p> <p>thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload