Reputation: 3325
The following code receives a post request (using the express module), creates a new post request and passes it to another handler:
app.post('/commit', function (req, res) {
....
var payload = {
....
};
request({
method:'POST',
body:"payload=" + escape(JSON.stringify(payload)),
headers:{ '...' },
url:publicUrl
}, function (err, res, body) {
if (err) {
return console.error(err);
}
console.log(res.statusCode);
});
res.writeHead(200, { 'Content-Type': 'application/json' });
var obj = {};
obj['Status'] ="don't know how to get the access code";
res.end( JSON.stringify( obj ) );
},
Now I want to be able to add the actual status code to the json, but I don't know how to access it since I'm sort of in a different scope, right?
Thanks, Li
Upvotes: 1
Views: 2653
Reputation: 26690
My first thought would be to try something like this (notice how I moved the code that builds the response to be inside the callback from the POST request):
app.post('/commit', function (req, res) {
....
var payload = {
....
};
request({
method:'POST',
body:"payload=" + escape(JSON.stringify(payload)),
headers:{ '...' },
url:publicUrl
}, function (err, postRes, body) {
if (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
var obj = {};
obj['Status'] = 'something went wrong: ' + err;
res.end( JSON.stringify( obj ) );
}
else {
res.writeHead(200, { 'Content-Type': 'application/json' });
var obj = {};
obj['Status'] = postRes.statusCode;
res.end( JSON.stringify( obj ) );
}
});
},
Upvotes: 1