proxy.js
const express = require('express');
const request = require('request');
const router = express.Router();
const Busboy = require('busboy');
exports.requestMapping = '/proxy';
router.post('/file/upload', (req, res) => {
const busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('data', function(data) {
uploadFileSize = data.length;
});
});
const sreq = request.post(`${CONFIG.api.接口Api}/接口路径`, {
'content-type': req.get('content-type'),
}, (err, res, body) => {
if (err) {
} else {
try {
console.log(body)
body = JSON.parse(body);
} catch (err) {
}
}
});
req.pipe(busboy);
req.pipe(sreq);
sreq.pipe(res);
});
upload.jsx
// 上传文件
uploadFile = (file, fileName) => {
let formData = new FormData();
formData.append('file', file, fileName);
this.request.post('/proxy/file/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
}).then(result => {
console.log(result)
if (parseInt(result.code) === 200) {
} else {
}
}).catch(err => {
});
};