我有这段代码
const express = require('express')
const app = express()
app.get('/', function (req, res) {
var process = require('child_process');
process.exec('hadoop fs -ls',function (err,stdout,stderr) {
if (err) {
console.log(typeof(stderr));
console.log("\n"+stderr);
res.send(stderr);
} else {
console.log(stdout);
}
});
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
我希望它可以将cmd输出发送回客户端,但是我收到了这个错误:
C:\Users\Administrator\Desktop\CSSE 434\project\backend>node index.js
Example app listening on port 3000!
string
'hadoop' is not recognized as an internal or external command,
operable program or batch file.
_http_outgoing.js:371
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\Administrator\Desktop\CSSE434\project\backend\node_modules\express\lib\response.js:767:10)
at ServerResponse.contentType (C:\Users\Administrator\Desktop\CSSE 434\project\backend\node_modules\express\lib\response.js:595:15)
at ServerResponse.send (C:\Users\Administrator\Desktop\CSSE 434\project\backend\node_modules\express\lib\response.js:145:14)
at C:\Users\Administrator\Desktop\CSSE434\project\backend\index.js:10:17
at ChildProcess.exithandler (child_process.js:212:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:194:7)
at maybeClose (internal/child_process.js:899:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
如您所见,输出string
表明stderr是一个字符串类型
hadoop is not recognized
是正常的,因为我本地没有hadoop
但我无法弄清楚为什么它不会将结果发送给我的客户。
有人可以帮我这个吗?
PS:这是我项目中唯一的文件。没有其他文件可供展示
答案 0 :(得分:0)
好吧,我只需要返回res.send(stderr);当它试图发送" hello world"时会发生错误。发送stderr后