错误:发送后无法设置标题。 的node.js

我正在努力coed一个应用程序,让用户通过url执行命令,但我得到这个错误消息:

_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); 

每个http请求只能调用res.json

更改

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

至:

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