如何修改节点js响应

How to modify node js response

我正在使用节点 js 作为使用 NTLM 身份验证的休息服务的代理。 我使用 httpntlm 模块来绕过 NTLM 身份验证。此模块发出额外请求并 returns 响应。

如何将 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);
    });
});

在您提供的示例代码中,使用了 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
    });
});