How it works…

When we call the .exec() method, a separate shell is created, and the command runs in it. If the call is successful, the output will be returned; otherwise, an object with a .stderr property will be thrown as an exception. A possible couple of runs are as follows:

getDirectoryJs("/home/fkereki/MODERNJS/chapter03/flow-typed/npm");
OUT /home/fkereki/MODERNJS/chapter03/flow-typed/npm
-rw-r--r-- 1 fkereki users 4791 Apr 9 12:52 axios_v0.18.x.js
-rw-r--r-- 1 fkereki users 3006 Mar 28 14:51 babel-cli_vx.x.x.js
-rw-r--r-- 1 fkereki users 3904 Apr 9 12:52 babel-eslint_vx.x.x.js
-rw-r--r-- 1 fkereki users 2760 Apr 9 12:52 babel-preset-env_vx.x.x.js
-rw-r--r-- 1 fkereki users 888 Apr 9 12:52 babel-preset-flow_vx.x.x.js
-rw-r--r-- 1 fkereki users 518 Apr 9 12:52 eslint-config-recommended_vx.x.x.js
-rw-r--r-- 1 fkereki users 14995 Apr 9 12:52 eslint-plugin-flowtype_vx.x.x.js
-rw-r--r-- 1 fkereki users 73344 Apr 9 12:52 eslint_vx.x.x.js
-rw-r--r-- 1 fkereki users 1889 Mar 28 14:51 fetch_vx.x.x.js
-rw-r--r-- 1 fkereki users 188 Apr 9 12:52 flow-bin_v0.x.x.js
-rw-r--r-- 1 fkereki users 13290 Apr 9 12:52 flow-coverage-report_vx.x.x.js
-rw-r--r-- 1 fkereki users 1091 Apr 9 12:52 flow-remove-types_vx.x.x.js
-rw-r--r-- 1 fkereki users 5763 Apr 9 12:52 flow-typed_vx.x.x.js
-rw-r--r-- 1 fkereki users 1009 Apr 9 12:52 mariasql_vx.x.x.js
-rw-r--r-- 1 fkereki users 0 Mar 28 14:51 moment_v2.3.x.js
-rw-r--r-- 1 fkereki users 5880 Apr 9 12:52 nodemon_vx.x.x.js
-rw-r--r-- 1 fkereki users 4786 Apr 9 12:52 prettier_v1.x.x.js

getDirectoryJs("/boot");
ERR ls: cannot access '*.js': No such file or directory

The second parameter for .exec() provides an object with possible options. In our case, we are specifying the current working directory (cwd) for the command. Another interesting option can let you work with commands that produce lots of output. By default, the maximum buffered output will be 200K; if you need more, you'll have to add an object with the maxBuffer option set to a larger value; check https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback for more on these and other options.

There's no limit to the complexity of the command that you can execute, but there's also a risk. Keep in mind the possibility of being hacked: if you are building up your command based on some input provided by an user, you could be on the end of a command injection attack. Imagine you wanted to build something such as `ls ${path}` and the user had provided "/; rm -rf *" as the path; what would happen?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.233.43