问题
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.
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?
Server A Routes:
'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;
Server 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');
}
};
Server B Routes:
'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;
Server B processFile:
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.
}
};
回答1:
The encoding that you are using so send the file may not be multipart/form-data
. Try setting that header in your file send request. Express req.files
will only be populated with a multipart/form-data
Express req.files will only be populated with a multipart/form-data . You're already using multer so I'm guessing the sending encoding is not quite right.
RP({
method: "POST",
uri: "serverB.net",
form: {
file: file.buffer
},
headers: {
"Content-Type": "multipart/form-data"
}
});
回答2:
router.post('/process', process.single('file'), processFile);
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);
})
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.
来源:https://stackoverflow.com/questions/35612622/transfer-files-between-two-node-js-servers-over-http