通过http在两个node.js服务器之间传输文件 [英] transfer files between two node.js servers over http
问题描述
我有两个node.js/express服务器,它们通过http相互通信. 服务器A 还可与浏览器通信,并可以处理文件上传请求.当文件上传到服务器A 时,我想按原样将其传输到服务器B 进行进一步处理.最好的方法是什么?最好与 request-promise 模块配合使用两台服务器之间的通信.
I have two node.js/express servers that communicate with each other with http. server A is also communicates with the browser and can handle file upload requests. When file is uploaded to server A i want to transfer it as is to server B for further processing. what is the best way to do so? preferably with request-promise module which is what i'm using for communication between the two servers.
到目前为止,这是我得到的,但是我无法在服务器之间传输文件,文件已成功上传到服务器A ,并且在将文件发送到
This is what i got so far, but i am unable to transfer the file between the servers, the file is uploaded successfully to server A, and there is no Error while sending it to server B
, but server B is doesn't recognise the request as file. what am i missing here?
服务器A路由:
'use strict';
// Routes
const express = require('express');
const router = express.Router();
const multer = require('multer');
const upload = multer();
const uploadController = require('./controllers/file/upload');
router.post('/upload',upload.single('file'),uploadController);
module.exports = router;
服务器A uploadController:
'use strict';
const RP = require('request-promise');
module.exports = (req, res) => {
const body = req.body;
if(req.file) {
const file = req.file;
RP('http://serverB.net/process', {
method: 'POST',
formData: {file_buffer: file.buffer},
body: body
})
.then((response) => {
return res.status(200).send(response);
})
.catch((e) => {
return res.status(500).send(e.message);
})
}
else {
return res.status(500).send('unable to upload file');
}
};
服务器B路由:
'use strict';
// Routes
const express = require('express');
const router = express.Router();
const multer = require('multer');
const process = multer();
const processFile = require('./controllers/file/processFile');
router.post('/process', process.single('file'), processFile);
module.exports = router;
服务器B的进程文件:
在这里我想处理服务器A 中的文件,但req.file
是undefined
here i want to process the file from Server A but req.file
is undefined
'use strict';
module.exports = (req, res) => {
if(req.file) {
// here i want to do something with the file.
}
};
推荐答案
router.post('/process', process.single('file'), processFile);
此行,特别是process.single('file')
,告诉multer查找将包含实际文件数据的字段file
.但是,在您的请求中,您永远不会为file
指定值.在您的请求中将名称file_buffer
更改为仅file
.或将您的process.single()
更改为process.single('file_buffer')
This line, specifically process.single('file')
tells multer to look for a field file
which will contain the actual file data. However, in your request you never specify a value for file
. Change the name file_buffer
to just file
in your request. Or change your process.single()
to process.single('file_buffer')
RP('http://serverB.net/process', {
method: 'POST',
formData: {
file: file.buffer,
body: body
}
})
.then((response) => {
return res.status(200).send(response);
})
.catch((e) => {
return res.status(500).send(e.message);
})
现在在服务器B上的processFile
内,您应该看到一个req.body
字段,其中包含body
字段,该字段包含在请求中传递的整个正文,并且现在应该有一个req.file
字段,其中包含了multer文件对象.
Now on Server B, inside processFile
you should see a req.body
with a field body
that contains your entire body passed in your request and you should have a req.file
now that contains your multer file object.
这篇关于通过http在两个node.js服务器之间传输文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!