Tuesday, 19 May 2020

Problem with child_process.exec() args when called from an Express route

For this example, I'm running Node 12.14.1 on a Debian platform and calling out to pdflib to merge PDFs. The following code works fine if run in an ordinary Node context/script;let cmd = 'pdftk'; let args = [ '"/path/to/file/with\ space\ in\ name_1.pdf"', '"/path/to/file/with\ space\ in\ name_2.pdf"', 'cat', 'output', '/path/to/output.pdf' ]; let command = [cmd, ...args].join(' '); const { stdout, stderr } = await exec(command); But, if run within the context of an Express route/request, something happens to the file paths here and pdftk ends up telling me the "Input must be a string". NOTE: this does NOT happen if the file paths do no contain spaces. So it's definitely some sort of shell escaping issue. The child_process.exec() documentation states that:// Double quotes are used so that the space in the path is not interpreted as // a delimiter of multiple arguments.So I'm wrapping the two file name arguments in double quotes. Again, this works fine when being called directly from the Node script. It only becomes an issue when called via an Express route.There's nothing different about the code. Literally copy/paste, works one place and not the other. Any thoughts?

Submitted May 19, 2020 at 02:14PM by khoker

No comments:

Post a Comment