Skip to content

@pnp/nodejs/proxy

Added in 1.3.2

In some cases when deploying on node you may need to use a proxy as governed by corporate policy, or perhaps you want to examine the traffic using a tool such as Fiddler. In the 1.3.2 relesae we introduced the ability to use a proxy with the @pnp/nodejs library.

Basic Usage

You need to import the new setProxyUrl function from the library and call it with your proxy url. Once done an https-proxy-agent will be used with each request. This works across all clients within the @pnp/nodejs library.

import { SPFetchClient, SPOAuthEnv, setProxyUrl } from "@pnp/nodejs";

sp.setup({
    sp: {
        fetchClientFactory: () => {

            // call the set proxy url function and it will be used for all requests regardless of client
            setProxyUrl("{your proxy url}");
            return new SPFetchClient(settings.testing.sp.url, settings.testing.sp.id, settings.testing.sp.secret, SPOAuthEnv.SPO);
        },
    },
});

Use with Fiddler

To get Fiddler to work you may need to set an environment variable. This should only be done for testing!

import { SPFetchClient, SPOAuthEnv, setProxyUrl } from "@pnp/nodejs";

sp.setup({
    sp: {
        fetchClientFactory: () => {

            // ignore certificate errors: ONLY FOR TESTING!!
            process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

            // this is my fiddler url locally
            setProxyUrl("http://127.0.0.1:8888");
            return new SPFetchClient(settings.testing.sp.url, settings.testing.sp.id, settings.testing.sp.secret, SPOAuthEnv.SPO);
        },
    },
});
spacer