javascript - Error: Can't set headers after they are sent. node.js -
i'm tring coed application lets users execute commands on url error message:
_http_outgoing.js:346 throw new error('can\'t set headers after sent.'); ^error: can't set headers after sent. @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:346:11) @ serverresponse.header (c:\users\jarvis\node_modules\express\lib\response.js:718:10) @ serverresponse.json (c:\users\jarvis\node_modules\express\lib\response.js:246:10) @ c:\users\jarvis\desktop\sys.js:9:6 @ childprocess.exithandler (child_process.js:193:7) @ emittwo (events.js:100:13) @ childprocess.emit (events.js:185:7) @ maybeclose (internal/child_process.js:850:16) @ process.childprocess._handle.onexit (internal/child_process.js:215:5)
this code:
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);
you can call res.json
1 time per http request.
change
res.json({"stdout":stdout}); res.json({"stderr": stderr});
to:
res.json({"stdout":stdout, "stderr": stderr});
Comments
Post a Comment