multer-proxy-storage-engine
v1.1.2
Published
Forward multipart/form-data file upload into a remote server.
Downloads
2
Readme
Multer Proxy Storage Engine
This is a custom multer storage engine that proxy the received file into a remote server. The proxy is forwarded as multipart/form-data.
Install
npm install --save multer multer-proxy-storage-engine
Usage
In this example we are forwarding user uploaded file into remote server http://www.example.com/upload, with the file identified with the parameter 'file'.
Axios (https://www.npmjs.com/package/axios)[https://www.npmjs.com/package/axios] library is used for proxy request. multer-proxy-storage-engine can foward all options for Axios request (https://www.npmjs.com/package/axios#request-config)[https://www.npmjs.com/package/axios#request-config]
const multer = require('multer')
const multerProxyStorage = require('multer-proxy-storage-engine')
this.routePost('/uploadFile',
(req, res, next) => {
multer({
storage: multerProxyStorage(
{
url: 'http://www.example.com/upload', // REQUIRED
method: 'post', // OPTIONAL (if not set, deafult value is 'post')
fileParamName: 'file' // OPTIONAL (if not set, deafult value is 'file') - this is a outgoing proxy file param name
timeout: 5000 // in ms, OPTIONAL (if not set, deafult value is 5000)
// **** in addtion, you can set all (Axios configurations)[https://www.npmjs.com/package/axios#request-config] here
}),
preservePath: true
}).array('file')(req, res, next); // 'file' is a incoming request param name for the multer configuration
}, (req, res, next) => {
res.send('Success!')
})
Or it can be used as following
const multer = require('multer')
const multerProxyStorage = require('multer-proxy-storage-engine')
const upload = multer({
storage: multerProxyStorage(
{
url: 'http://www.example.com/upload', // REQUIRED
method: 'post', // OPTIONAL (if not set, deafult value is 'post')
fileParamName: 'file', // OPTIONAL (if not set, deafult value is 'file') - this is a outgoing proxy file param name
timeout: 5000, // in ms, OPTIONAL (if not set, deafult value is 5000)
// **** in addtiona, you can set all (Axios configurations)[https://www.npmjs.com/package/axios#request-config] here
headers: {'X-Requested-With': 'XMLHttpRequest'} // an example of Axios configuration usage
}),
preservePath: true
}).array('file'); // NOTE: you can use .single('file') instead of .array('file') if you are expecting only one file
upload(req, res, (err, resData) => {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading. (https://github.com/expressjs/multer/blob/master/lib/multer-error.js)[https://github.com/expressjs/multer/blob/master/lib/multer-error.js]
res.send('Multer ERROR');
console.error(err);
} else if (err) {
// An unknown error occurred when uploading.
res.send('Unknown ERROR');
console.error(err);
} else {
res.send('SUCCESS');
console.log(resData);
}
});
License
Multer-Proxy-Storage-Engine is released under the MIT license.