将文件从 express js 传输到另一台服务器
Transferring Files from express js to another server
我有一种情况,我正在从 Jquery 向我的 express 服务器发送一个文件,我需要将该请求传输到另一个服务器,而不需要在 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" });
};
问题是我没有在第二台服务器上获取表单数据。
任何建议都是 helpful.Thanks
我建议使用 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!
我有一种情况,我正在从 Jquery 向我的 express 服务器发送一个文件,我需要将该请求传输到另一个服务器,而不需要在 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" });
};
问题是我没有在第二台服务器上获取表单数据。 任何建议都是 helpful.Thanks
我建议使用 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!