node-http-proxy

Proxied HTTP response being modified


I have a gulpfile that launches a dev proxy server.

gulp.task('dumbserver', ()=> {
    const express = require('express');
    const httpProxy = require('http-proxy');
    const app = express();
    const proxy = httpProxy.createProxyServer();

    app.use('/api', function (req, res) {
        proxy.web(req, res, {target: 'https://bos1-vcd-sp-static-199-8.eng.vmware.com/api', secure: false},
            (e) => console.log('error', e)
        );
    });
    return app.listen(8080, function () {
        console.log('Server started on port 8080.');
    });
});

When I make a call on postman directly to https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session I get the following payload:

<Session locationId="86171c79-e8f0-4c06-a0a8-4bc7fde76915@7bcf706c-d90f-4e1b-b0cc-b2a13db3e618" org="juan" roles="System Administrator" user="administrator" userId="urn:vcloud:user:7b5f0241-f597-4851-8cae-655a15afde24" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session" type="application/vnd.vmware.vcloud.session+xml"> <Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/org/" type="application/vnd.vmware.vcloud.orgList+xml"/> <Link rel="remove" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/session"/> <Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/admin/" type="application/vnd.vmware.admin.vcloud+xml"/> <Link rel="down" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/api/admin/extension" type="application/vnd.vmware.admin.vmwExtension+xml"/> <Link rel="nsx" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/network" type="application/xml"/> <Link rel="openapi" href="https://bos1-vcd-sp-static-199-8.eng.vmware.com/cloudapi" type="application/json"/> </Session>

However, when I make a call using postman to localhost:8080/api/session, the last two links have the hrefs rewritten to be

<Link rel="nsx" href="https://localhost:8080/network" type="application/xml"/>
<Link rel="openapi" href="https://localhost:8080/cloudapi" type="application/json"/>

But none of the other links have been rewritten.

Any suggestions about what could be going on?


Solution

  • It turns out that http-proxy adds a HOST header. The server side code that was generating the links was using that HOST header if available as the base URL, overriding the configured value.

    Not deleting the question in the hopes that the knowing fact that http-proxy adds a host header to requests may cause a bug in your code.