Skip to content

Instantly share code, notes, and snippets.

@smmoosavi
Last active June 15, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smmoosavi/66584aaa234efa28b527b5afde643596 to your computer and use it in GitHub Desktop.
Save smmoosavi/66584aaa234efa28b527b5afde643596 to your computer and use it in GitHub Desktop.
Proxy example
yarn add http-proxy-middleware express
node proxy.js
var express = require('express');
var proxy = require('http-proxy-middleware');
var app = express();
var craPattern = /^(\/(static\/|.*\.hot-update\.js(on)?$|sockjs-node\/)).*$/;
app.use(craPattern, proxy({
target: 'http://localhost:3000', // create react app
ws: true,
changeOrigin: true
}));
app.use(proxy({
target: 'http://localhost:8000', // django
changeOrigin: true
}));
app.listen(4000);
@wandss
Copy link

wandss commented Jun 15, 2018

Hi smmoosavi, how are you?
May you please explain how does this proxy works?
Should I run "npm start" and "manage.py runserver" an then, in browser call localhost:4000 ?
The idea is to make requests from 3000 run like they're made directly from Django?

To achieve this I've made a Python script that tracks changes within the react app. Whenever there's any change, this python script runs "npm run build" and copies the main.js file to Django HOME so it can be served.
The problem is that the building process takes quite some time (Still testing though).

If this proxy idea works it is sure much better than mine.
Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment