yarn add http-proxy-middleware express
node proxy.js
Last active
June 15, 2018 02:33
-
-
Save smmoosavi/66584aaa234efa28b527b5afde643596 to your computer and use it in GitHub Desktop.
Proxy example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.