node.js send form data to another server

試著忘記壹切 提交于 2021-01-29 14:25:20

问题


I received the file from the front-end no problem but then when I try to send it to another server, I get a "413 Request Entity Too Large" in the best case scenario.

Does anyone have an idea what I'm doing wrong? I have also tried with axios... getting the same issue

Thanks, Guys

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}`);
});

Sending data

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")
        });


}

来源:https://stackoverflow.com/questions/53590923/node-js-send-form-data-to-another-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!