javascript - Node.js http server, function can't access response object -
i have following http server code
var server = http.createserver(function (req, response) { body = ""; req.on("data", function (data) { body += data; }); req.on("end", function (){ parserequest(body); }); }).listen(8080); var parserequest = function (data) { try { jsondata = json.parse(data); handlerequest(jsondata); } catch (e) { console.log("json parse failed") console.log(e); response.writehead(500); response.end("json parse failed"); } }
i thought parserequest function should able access it's variables in in parent function's scope. there doing wrong here?
the error is,
response.writehead(500); ^ referenceerror: response not defined
change to:
req.on("end", function (){ parserequest(response, body); });
and then:
var parserequest = function (response, data) { ...
and can access response. ;)
Comments
Post a Comment