【问题标题】:Transferring Files from express js to another server将文件从 express js 传输到另一台服务器
【发布时间】:2016-12-17 09:53:36
【问题描述】:

我有一种情况,我将文件从 Jquery 发送到我的快速服务器,我需要将该请求传输到另一台服务器而不在快速服务器中解析文件。这是我到目前为止使用的代码 sn-ps jQuery

$.ajax({
            url: 'api/auth/handle',
            type: 'POST',
            data: data, // formData
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function (data, textStatus, jqXHR) {
                console.log("success");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // Handle errors here
                console.log('ERRORS: ' +textStatus);
                // STOP LOADING SPINNER
            }
        });

表达js代码

module.exports.handleFile = (req, res) => {
    console.log(req);
    let data = request.post("http://localhost:5002/files/pdf", function (err, resp, body) {
        //console.log(err);
        if (err) {
            console.log('Error!');
        } else {
            console.log('URL: ' + body);
        }
    });
    let form = data.form();
    form.append('file', '<FILE_DATA>', req);
    res.status(200).send({ "success": "success" });

};

问题是我没有在我的第二台服务器上获取表单数据。 任何建议都会有所帮助。谢谢

【问题讨论】:

    标签: javascript jquery node.js express multipartform-data


    【解决方案1】:

    我建议为 Node.js/Express 使用代理模块。其中一个可用的称为express-http-proxy

    他们的文档中的示例用法:

    var proxy = require('express-http-proxy');
    
    var app = require('express')();
    
    app.use('/proxy', proxy('www.google.com', {
      forwardPath: function(req, res) {
        return require('url').parse(req.url).path;
      }
    }));
    

    因此您可以将您选择的请求重定向到另一台服务器。

    更多信息请见https://github.com/villadora/express-http-proxy

    【讨论】:

    • 不设置代理还有其他方法吗?可能是通过某种方式使用请求模块?
    • 对不起,我不知道有什么聪明的、简短的解决方案可以达到同样的效果。代理方式是我要走的路,但也许有人有不同的解决方案。
    • 我一定会试试代理。谢谢
    • 是的,代理是一种解决方案,仍在寻找没有代理的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2012-05-29
    相关资源
    最近更新 更多