Skip to content

Instantly share code, notes, and snippets.

@normal-carrot
Last active February 16, 2021 10:52
Show Gist options
  • Select an option

  • Save normal-carrot/3a5e68acc2b1ee69ed49b6a3eaee094a to your computer and use it in GitHub Desktop.

Select an option

Save normal-carrot/3a5e68acc2b1ee69ed49b6a3eaee094a to your computer and use it in GitHub Desktop.

Revisions

  1. Alexander Kohonovsky revised this gist Sep 1, 2018. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions proxy-middleware.js
    Original file line number Diff line number Diff line change
    @@ -36,13 +36,16 @@ function removeSecureCookieFlag(proxyRes, req, res) {
    }
    }

    // https://expressjs.com/en/4x/api.html#app.use
    const app = express();

    // Static content if needed
    // app.use(express.static('build'));
    // Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.

    app.use('/api/*', proxy(proxyContexts.api));
    app.use('/api', proxy(proxyContexts.api));
    app.use(proxy('ws://localhost:3000'));
    app.use(proxy(proxyContexts.react));

    // Static content if needed
    // app.use(express.static('build'));

    app.listen(3100);
  2. Alexander Kohonovsky revised this gist Sep 1, 2018. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions proxy-middleware.js
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,11 @@ const proxy = require('http-proxy-middleware');
    const proxyContexts = {
    react: {
    target: 'http://localhost:3000',
    changeOrigin: true
    //changeOrigin: true
    },
    api: {
    target: 'http://localhost:8080',
    changeOrigin: true
    //changeOrigin: true
    },
    prod: {
    target: 'https://my-prod.com',
    @@ -41,10 +41,8 @@ const app = express();
    // Static content if needed
    // app.use(express.static('build'));

    // Redirect to create-react-app
    app.use('/', proxy(proxyContexts.react));

    // Redirect to api
    app.use('/api', proxy(proxyContexts.api));
    app.use('/api/*', proxy(proxyContexts.api));
    app.use(proxy('ws://localhost:3000'));
    app.use(proxy(proxyContexts.react));

    app.listen(3100);
  3. Alexander Kohonovsky created this gist Sep 1, 2018.
    25 changes: 25 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    {
    "name": "test",
    "version": "0.1.0",
    "private": true,
    "scripts": {
    "app": "concurrently \"node proxy-middleware.js\" \"npm start\"",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
    },
    "dependencies": {
    "npm": "^6.4.1",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4"
    },
    "devDependencies": {
    "concurrently": "^4.0.1",
    "express": "^4.16.3",
    "http-proxy-middleware": "^0.19.0"
    }
    }
    50 changes: 50 additions & 0 deletions proxy-middleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    const express = require('express');
    const proxy = require('http-proxy-middleware');

    const proxyContexts = {
    react: {
    target: 'http://localhost:3000',
    changeOrigin: true
    },
    api: {
    target: 'http://localhost:8080',
    changeOrigin: true
    },
    prod: {
    target: 'https://my-prod.com',
    changeOrigin: true,
    onProxyRes: removeSecureCookieFlag
    }
    };

    /**
    * Remove "secure" flag from set-cookie header.
    * Insecure sites (http:) can't set cookies with the "secure" directive anymore (since Chrome 52+ and Firefox 52+).
    */
    function removeSecureCookieFlag(proxyRes, req, res) {
    let existingCookies = proxyRes.headers['set-cookie'];
    let rewrittenCookies = [];

    if (existingCookies !== undefined) {
    if (!Array.isArray(existingCookies)) {
    existingCookies = [existingCookies];
    }
    for (let i = 0; i < existingCookies.length; i++) {
    rewrittenCookies.push(existingCookies[i].replace(/;\s*?(Secure)/i, ''));
    }
    proxyRes.headers['set-cookie'] = rewrittenCookies;
    }
    }

    const app = express();

    // Static content if needed
    // app.use(express.static('build'));

    // Redirect to create-react-app
    app.use('/', proxy(proxyContexts.react));

    // Redirect to api
    app.use('/api', proxy(proxyContexts.api));

    app.listen(3100);