使用node/express.js和节点请求代理文件上传导致损坏的文件,几乎使文件大小混乱 [英] Proxying file upload with node/express.js and node request results in corrupt files with almost dobble the file size

查看:66
本文介绍了使用node/express.js和节点请求代理文件上传导致损坏的文件,几乎使文件大小混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过node.js/express.js服务器代理我们的主服务器.除了文件上传,我已经一切正常.文件已上传并到达服务器,但是在传递到node.js请求的数据与到达服务器的数据之间的某个位置文件已损坏.

I'm trying to proxy our main server via a node.js/express.js server. I've got everything working as it should except file uploads. Files are uploaded and reach the server, however somewhere between the data being passed over to node.js request and the data reaching the server the file is corrupted.

内容长度为1833的文件在到达服务器后最终的内容长度为3274.我希望到达服务器的文件保持在1833年.不使用代理运行就可以按预期工作.

A file with content-length 1833 ends up having a content-length 3274 once it reaches the server. I expect the file that reaches the server to stay at 1833. Running without the proxy works as expected.

这是代理POST端点

router.post("*", wrapAsync(async (req: any, res: any) => {
    const path = ROOT_PATH + req.originalUrl;
    logger.info("Proxy request to: POST " + path);
    const headers = JSON.parse(JSON.stringify(req.headers)); // create a copy of headers
    delete headers["content-length"]; // Without this node.js requests will set the content-length to null
    if (req.session.auth) {
        headers.Authorization = req.session.auth.Token;
    }
    const options = {
        agentOptions,
        body: req.rawBody,
        encoding: null as any,
        headers,
        json: false,
        resolveWithFullResponse: true,
        url: path
    };
    const result = await request.post(options);
    handleJSONResponse(result, res);
}));

req.rawBody的值是在以下小型中间件中创建的:

The value for req.rawBody are created in the following little middleware:

app.use("/rest/api", (req: any, res, next) => {
    // This is a special piece of proxy middleware that provides the raw buffer
    req.rawBody = "";
    req.setEncoding("utf8");
    req.on("data", (chunk: any) => {
        req.rawBody += chunk;
    });
    req.on("end", () => {
        next();
    });
}, proxyRouter);

这可能是中间件中的某物或node.js请求中的某物(错误或我对其配置方式的某物)似乎导致了此问题.只要文件被POST:到服务器中的同一端点,我就可以在单独的端点中处理文件(即,不必直接代理,我可以控制前端使用的URL)

It is either something in the middleware or something in node.js request (either a bug or something in how I've configured it) that seems to be causing the issue. It is possible for me to handle the files in a separate endpoint so long as it gets POST:ed to the same endpoint in the server (ie, it does not have to be directly proxied, I have control what URLS the frontend uses)

推荐答案

问题出在中间件中

(req: any, res, next) => {
    // This is a special piece of proxy middleware that provides the raw buffer
    req.rawBody = "";
    req.setEncoding("utf8");
    req.on("data", (chunk: any) => {
        req.rawBody += chunk;
    });
    req.on("end", () => {
        next();
    });
}

使用bodyParser.raw()解决了该问题,即:

Solved it by using bodyParser.raw() instead, ie:

app.use("/rest/api", bodyParsers.raw(), proxyRouter);

这篇关于使用node/express.js和节点请求代理文件上传导致损坏的文件,几乎使文件大小混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆