node.js将表单数据发送到另一台服务器

时间:2018-12-03 09:36:38

标签: node.js axios form-data request-promise

我从前端毫无问题地收到了文件,但是当我尝试将其发送到另一台服务器时,在最佳情况下会收到“ 413 Request Entity Too Large”。

有人知道我在做什么错吗?我也尝试过axios ...遇到相同的问题

谢谢,伙计们

index.js

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT, PATCH, OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, api_key, Authorization'); 
    res.setHeader('Access-Control-Expose-Headers', 'Content-Range');
    next();
  }); 

app.use('/', express.static('public'))


app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({extended: true, limit: '50mb'}));


app.use('/', routes);

const port = config["server.port"];

app.listen(port, () => {
    console.log(`Magic happens on port ${port}`);
});

发送数据

exports.uploadFile = async (req, res) => {

    const verifyToken = require("../controllers/helpers/backOfficeToken")

    const token = await verifyToken.verify(req, res)

    if (token === undefined) {
        return;
    }

    var rp = require('request-promise');

    const { backEndToken } = await getCreditentials.verify(req, res);

    const { userId } = req.body;

    const options = (url) => {
        return {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${backEndToken}`
            },
            formData: {
                file: req.files[0].buffer,
            },
            uri: baseUrl + url + userId
        }
    };

    rp(options("/wallet-service/certificates/"))
        .then(function (body) {
            console.log(body)
        })
        .catch(function (err) {
            console.log(err);
            res.send("an error has occured")
        });


}

0 个答案:

没有答案