将文件从快速js传输到另一台服务器

时间:2016-12-17 09:53:37

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

我遇到的情况是我从Jquery发送文件到我的express服务器,我需要将该请求转移到另一台服务器而不解析快速服务器中的文件。这是我到目前为止使用的代码片段 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" });

};

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

1 个答案:

答案 0 :(得分: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