【问题标题】:How to modify node js response如何修改节点js响应
【发布时间】:2015-07-03 12:19:26
【问题描述】:

我使用 node js 作为使用 NTLM 身份验证的 rest 服务的代理。 我使用httpntlm 模块绕过 NTLM 身份验证。该模块发出附加请求并返回响应。

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

【问题讨论】:

  • 用这个res.status(ntlmRes.statusCode).send(ntmlRes.body)替换下一个和res对象更改
  • @RistoNovik 比你好多了!有用!您能否发表您的评论作为答案? - 我会将其标记为正确

标签: node.js ntlm


【解决方案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
    });
});

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2017-09-24
    • 2012-05-22
    • 1970-01-01
    • 2020-09-08
    • 2015-08-19
    • 2017-09-24
    • 2021-01-03
    相关资源
    最近更新 更多