繁体   English   中英

错误:发送后无法设置标头。 节点.js

[英]Error: Can't set headers after they are sent. node.js

我正在尝试编写一个允许用户通过 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});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM