2015-07-03 84 views
1

我正在使用節點js作爲使用NTLM身份驗證的休息服務的代理。 我使用httpntlm模塊來繞過NTLM身份驗證。該模塊發出附加請求並返回響應。如何修改節點js響應

如何將NTLM響應數據寫入原始響應?

var httpntlm = require('httpntlm'); 
var request = require('request'); 

app.use(function (req, res, next) { 
    httpntlm.post({ 
     url: url, 
     username: username, 
     password: password, 
     workstation: '', 
     domain: domain, 
     json: req.body 
    }, function (err, ntlmRes) { 

     // console.log(ntlmRes.statusCode); 
     // console.log(ntlmRes.body); 

     res.body = ntlmRes.body; 
     res.status = ntlmRes.statusCode; 

     next(); 
     // req.pipe(res); 
    }); 
}); 
+1

替換下一個和資源對象與此'res.status(ntlmRes.statusCode)。發送(ntmlRes.body)' –

+0

@RistoNovik比你非常多的變化!有用!你可以發表你的評論作爲答案嗎? - 我會將其標記爲正確的 – opewix

回答

1

在示例代碼中,你所提供的Express.js中間件使用,但簡單地調用手next()接管下一中間件和不輸出任何東西。相反,我們必須將回復發送給客戶。

var httpntlm = require('httpntlm'); 

app.use(function (req, res, next) { 
    httpntlm.post({ 
     url: url, 
     username: username, 
     password: password, 
     workstation: '', 
     domain: domain, 
     json: req.body 
    }, function (err, ntlmRes) { 

     // Also handle the error call 
     if (err) { 
      return res.status(500).send("Problem with request call" + err.message); 
     } 

     res.status(ntlmRes.statusCode) // Status code first 
      .send(ntmlRes.body);  // Body data 
    }); 
});