6

I've been hitting my head against this problem for a while and can't seem to figure it out. I'm trying to deploy a Django app to heroku where the front end uses react. I'm pretty new to react so it's possible I'm missing something fundamental here.

I have one heroku app right now that has two buildpacks (heroku/nodejs and heroku/python). To run the app locally I use python manage.py runserver along with node server.js on port 3000. I know Heroku only lets one port receive HTTP requests so I think the problem is there.

My main question is: To use Django and React on a Heroku app do I need to have two separate apps (one for django and one for react) or can I maintain only one app?

I've been searching through this repository and it seems like only one app is used. However I can't see how the node server is working.

Thanks for any clarification or a push in the right direction! Let me know if I need to clarify my question.

Edit: A couple other things I've tried:

  1. Following this tutorial
  2. Adding a heroku-postbuild script to my package.json file that starts the node server. This prevents the build process from continuing to start the django app though.
  3. I came across this blog post too but it seems like I'm getting conflicting information on if the apps can be in the same Heroku app or must be different (like in the tutorial from #1).
2
  • 1
    I've implemented React and Django in just one application, using Django Rest Framework for API calls from the front end. As @taylorc93 said, you only need Node for server side rendering. Commented Dec 20, 2016 at 22:08
  • Hey did you figure this out? I still am having issues... Commented Dec 16, 2020 at 4:45

1 Answer 1

5

Unless you plan on implementing server side rendering (not advisable if you're new to react), you only need one server.

What you're missing is a way to generate a javascript bundle that contains all of your react code. Then, you can ship that bundle over to the client like you would any other HTML/CSS/JS file.

Try using a tool like webpack to generate a javascript bundle. If possible, put it in the heroku-postbuild step, if not, just generate the bundle before deploying the updates

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response! I am currently using webpack along with django-webpack-loader. The bundles are generated but I don't believe I'm serving them correctly.
Hmmmm... Are you getting any error messages? Can you post your webpack config + relevant django settings (owaislone.org/blog/webpack-plus-reactjs-and-django/#settings-py)?
Do other static files get served? What if you just put a javascript file with console.log("hello world") and try to serve that in your HTML. Does it work? If not, the issue could be related to how Django serves static files.
If you end up doing server side rendering, you still only need one server. I wrote a blog post outlining this: blog.victorpontis.com/server-side-react

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.