37
So the issue was since both the Node dev environment and the Django dev environment were running in separate docker containers, so localhost
was referring to the node container, not the bridged network.
So the key was to use container links, which are automatically created when using docker-compose
, and use that as the hostname. So I changed it to
"proxy": {
"/api": {
"target": "http://django:8000"
}
},
And that worked, as long as you launch both containers with the same docker-compose
command, otherwise you have to manually specify external_links in your docker-compose.yml
file.
29
I faced a similar issue but in Mac machine. I changed localhost
to 127.0.0.1
in the package.json and that worked for me as below:
"proxy": "http://127.0.0.1:5000"
- [Django]-One app with many models vs. many apps with single model
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
- [Django]-How do you catch this exception?
15
Iβm running into the same problem as well. Most search results mention adding "secure": false
or "ignorePath": true
to your proxy config. Something like this:
"proxy": {
"/api/*": {
"target": "http://localhost:8000",
"secure": false
}
},
May be worth a try but unfortunately none of this worked for me. Although each address (http://localhost:3000 and http://localhost:8000) work completely fine in the browser, maybe since the container is actually proxying it needs to use a Docker address?
EDITβ
Alright I think I figured it out. I believe it did have to do with the container to container communication. Looking in your docker-compose
, your api server is called django
. Change your package.json file to this:
"proxy": {
"/api/*": {
"target": "http://django:8000",
"secure": false
}
}
- [Django]-How to reverse the URL of a ViewSet's custom action in django restframework
- [Django]-Django: no such table: django_session
- [Django]-What is the difference render() and redirect() in Django?
9
Didnβt actually get the answer here I was looking for but had an alternative solution work for me. I think itβs specifically related to Node v17, as thatβs when it started happening for me but the solution was pretty simple.
I updated:
"proxy": "http://localhost:8000"
To:
"proxy": "http://127.0.0.1:8000"
In case itβs relevant (I donβt think it is) β I was proxying to a Django server.
- [Django]-Is there a built-in login template in Django?
- [Django]-How to add url parameters to Django template url tag?
- [Django]-Django connection to PostgreSQL: "Peer authentication failed"
4
If you donβt feel like setting up docker compose, you can also use a docker network:
create network and run docker containers within that network
docker network create webapp_network
docker run -d -p 5000:5000 --name webapp_backend --network webapp_network webapp_backend_image
docker run -d -p 3000:3000 --name webapp_frontend --network webapp_network webapp_frontend_image
Added a line in package.json of my frontend React webapp:
"proxy": "http://webapp_backend:5000"
note you can now refer to your backend using the container name instead of localhost
- [Django]-How do I include related model fields using Django Rest Framework?
- [Django]-Getting the SQL from a Django QuerySet
- [Django]-Logging in Django and gunicorn
2
If your on a newer version CRA 2.0+ youβll need to do this via a manual proxy.
https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually
- [Django]-How to update user password in Django Rest Framework?
- [Django]-Can I have a Django form without Model
- [Django]-Django character set with MySQL weirdness
1
Could see the error after upgrading yesterday Docker
to version v19.03.13
(on Mac
), restarting Docker
fixed the issue. The application also runs Node.js
/React
, but not Django
. Basically, I had issues with connection to MongoDB Atlas
related to authentication/fetching anything from the cloud database.
- [Django]-Django β {% csrf_token %} was used in a template, but the context did not provide the value
- [Django]-Django models.py Circular Foreign Key
- [Django]-How to manually assign imagefield in Django
1
Choosing the exact value for localhost to populate the "target" property is mostly the solution (it can be localhost, 127.0.0.1, [::1] ).
A mac user should type in terminal to get the solution:
sudo lsof -iTCP -sTCP:LISTEN -n -P
- [Django]-How to use "AND" in a Django filter?
- [Django]-Django allauth social login: automatically linking social site profiles using the registered email
- [Django]-What is a "slug" in Django?
0
The correct answer will be to use manual proxy with
- target = docker address
django:4000
- correct HOST header
localhost:8000
because if Django uses reverse
function which returns absolute url
reverse('preview-mail', args=[mail.pk],request=request)
you need to have correct HOST header for it, or you may get the result URL like
https://django:4000/your-url`
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
proxy('/api', {
target: 'http://django:4000',
changeOrigin: true,
secure: false,
pathRewrite: {
'^/api': ''
},
onProxyReq: function (proxyReq, req, res) {
proxyReq.setHeader("host", 'localhost:8000')
}
})
)
}
- [Django]-Why does django ORM's `save` method not return the saved object?
- [Django]-Django order_by() filter with distinct()
- [Django]-Django Forms and Bootstrap β CSS classes and <divs>
0
If youβre working on a MERN stack app, make sure youβre not in the client folder. You need to be in the root. While in the root, run this command in the terminal. npm run start:dev
- [Django]-Django: change the value of a field for all objects in a queryset
- [Django]-Managing static files for multiple apps in Django
- [Django]-Django serializer Imagefield to get full URL
0
For NodeJs backend this can be solved by adding a line of code in the package.json file of the backend
"devstart": "nodemon server.js --ignore './location of frontend react directory'",
- [Django]-Django: manage.py does not print stack trace for errors
- [Django]-Override a form in Django admin
- [Django]-Django β Class Based Generic View β "No URL to redirect to"
0
I ran into this problem when I changed the webserver address.
At first I had a localhost, and everything worked fine. In order for me to access my web server from other machines, I changed its address to 0.0.0.0 after that I got this problem. It helped me to change the proxy settings from localhost to 127.0.0.1
- [Django]-Where can I find the error logs of nginx, using FastCGI and Django?
- [Django]-Using django-rest-interface
- [Django]-Chained method calls indentation style in Python
0
I had this same issue. my resolution was changing http://localhost:8000
to http://127.0.0.1:8000
- [Django]-How to automatically destroy django test database
- [Django]-Unable log in to the django admin page with a valid username and password
- [Django]-Get current user in Model Serializer
-3
In my case, my server was running on port 3001, but I had "proxy": "http://localhost:3000"
in the package.json file of my React fronted project
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-Explicitly set MySQL table storage engine using South and Django
- [Django]-Django error message "Add a related_name argument to the definition"