下記の正確なコードを含むjsファイルを実行すると、ENOENTを取得します。そして私はこれを解決する方法を知らない。要求(ENOENTの取得)でchild_process spawnの結果を使用する方法
説明やアドバイスは大歓迎ですが、私がそれをやろうとしている方法に対する解決策を希望します。
(私はもちろん、本当に愚かな何かをやっている場合を除き)私は、テスト目的のために、この偽の要求機能を書かれている:取得するchild_process.spawnを使用していますgetDiskInfo
機能を使用することになっている
(function imaginaryRequest(req, res)
{
getDiskInfo('/dev/simfs', info =>
{
console.log(info.ratio)
})
}())
df -Ph
出力し、オブジェクトに格納します。
/**
* Get information on the disk space of the specified File system
* @param {string} sysFile (ex: /dev/sda)
* @param {function} callback
* @return {object} df -Ph output stored into object
*/
function getDiskInfo(sysFile, callback) {
const spawn = require('child_process').spawn
const df_Ph = spawn("df -Ph | grep " + sysFile);
df_Ph.stdout.on('data', data => {
let info = data
.split(' ')
.filter(el => el != '')
callback({
maxSpace: info[1],
used: info[2],
unused: info[3],
ratio: info[4],
})
})
df_Ph.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
df_Ph.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
そして、ここでは、私はシェルで取得エラーです:
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn df -Ph | grep /dev/simfs ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:607:11)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
ありがとうございました。
ありがとうございました。最初の解決策では、バッファを取得するので、 '.split()'は機能しません。私はそれを私が推測する文字列にキャストする必要があります。 –
@RomainVincentこれは、いずれかの方法でバッファを取得すると予想されます。 'data.toString()'を使ってそのバッファを文字列に変換することができます。 – Paulpro