2012-08-27 32 views
1

下面的代碼接收一個POST請求(使用Express模塊​​)的響應,創建一個新的POST請求,並將其傳遞到另一個處理程序:的NodeJS表達如何得到一個嵌套的post請求

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)); 
}, 

現在我希望能夠將實際的狀態碼添加到JSON中,但是我不知道如何訪問它,因爲我處於不同的範圍,對吧?

感謝, 李

回答

0

我首先想到的是去嘗試這樣的事情(注意我是如何感動的是建立應對來自POST請求的回調中的代碼):

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

},