Vaadin-Upload not working with http-proxy-middleware

508 views Asked by At

I have a node.js / Polymer 1 website. I am using HTTP-proxy-middleware to route api calls (/api/webapi) to my backend API server.

On one of the pages I have a vaadin-upload (v2.3.0) component that sends files to the api. Everything appears to work fine when running on local host but when I deploy to our test servers I am experiencing issues. Either the upload completes quickly and then sits "processing" for a long time or it stalls.

Using postman I have managed to send a file to the API directly, to the proxy server. I have also managed to get the upload component to call the API directly. All these cases work correctly, and output from the API would suggest in all cases the API is receiving/processing data at the same rate. From this I have narrowed it down to an interaction between Vaadin-Upload and http-proxy-middleware.

Does anyone have experience with this and help me configure the proxy correctly.

proxy configuration:

const url = require('url');
var hpmproxy = require('http-proxy-middleware');
var config = require('../config');

// Adds user authorization token from passport to request
var addAuthTokenMiddleware = function (req, res, next) {

    if (req.session && req.isAuthenticated()) {
        req.headers['authorization'] = 'Bearer ' + req.user.token;
        next();
    } else {
        req.abort();
    }
};


function isLoggedIn(req, res, next) {
    // if user is authenticated in the session, carry on
    if (req.session && req.isAuthenticated())
        return next();

    res.status(403).end();
};

function restream(proxyReq, req) {
    if (isMultipartRequest(req))
        console.log('Multipart');
    if (!isEmpty(req.body)) {
        console.log("parse");
        var bodyData = JSON.stringify(req.body);
        proxyReq.setHeader('Content-Type', 'application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        proxyReq.write(bodyData);
    }
    console.log("-->[proxyReq]----", proxyReq.path, proxyReq.getHeader('Content-Type'));

};

function handleResponse(proxyRes, req, res) {
  console.log('---[proxyRes]<---', proxyRes.req.method, proxyRes.req.path, proxyRes.statusCode);
};

function isMultipartRequest(req) {
  let contentTypeHeader = req.headers['content-type'];
  return contentTypeHeader && contentTypeHeader.indexOf('multipart') > -1;
};

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }
    return JSON.stringify(obj) === JSON.stringify({});
}
var options = {
    target: config.webApiHost,

    changeOrigin: true, // needed for virtual hosted sites
    pathRewrite: {
        '^/api/webapi/': config.webApiPath
    },
    secure: !config.selfSigned,
    onProxyRes: handleResponse,
    onProxyReq: restream
   // ,logLevel: 'debug'


};

var hpmApiProxy = hpmproxy(options);

module.exports = function (app, passport, config) {
    app.use('/api/webapi/', isLoggedIn, addAuthTokenMiddleware, hpmApiProxy);
    console.log(' WebAPI Proxy Loaded');
}
0

There are 0 answers