2016-04-19 100 views
1

我特林男女同校的一個應用程序,它可以讓用戶通過URL執行命令,但我收到此錯誤信息:錯誤:發送後無法設置標題。 Node.js的

_http_outgoing.js:346 
throw new Error('Can\'t set headers after they are sent.'); 
^Error: Can't set headers after they are sent. 
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11) 
at ServerResponse.header (C:\Users\Jarvis\node_modules\express\lib\response.js:718:10) 
at ServerResponse.json (C:\Users\Jarvis\node_modules\express\lib\response.js:246:10) 
at C:\Users\Jarvis\Desktop\sys.js:9:6 
at ChildProcess.exithandler (child_process.js:193:7) 
at emitTwo (events.js:100:13) 
at ChildProcess.emit (events.js:185:7) 
at maybeClose (internal/child_process.js:850:16) 
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5) 

這是我的代碼:

var Express = require('express'); 
var app = Express(); 
var sys = require('sys'); 
var exec = require('child_process').exec; 
var child; 
app.get("/:cmd", function(req, res) { 
child = exec(req.params.cmd, function (error, stdout, stderr) { 
res.json({"stdout":stdout}); 
res.json({"stderr": stderr}); 
if (error != null) { 
    console.log("exec error: "+error); 
} 
}); 
}); 
app.listen(8080); 
+0

無法發送兩個響應。 – SLaks

回答

1

只能調用每個http請求一次有res.json

變化

res.json({"stdout":stdout}); 
res.json({"stderr": stderr}); 

到:

res.json({"stdout":stdout, "stderr": stderr}); 
相關問題