Home > Software design >  Does npm server is necessary for ReactJS with django on production?
Does npm server is necessary for ReactJS with django on production?

Time:11-01

I have developped the application with DJango ReactJS

when in developpment, using

$python manage.py runserver

and

$yarn start

which launche the two servers on localhost:8000 and localhost:3000

However in production environment, I need to launch two servers??

When I use webpack before I run two server in developpment environment, but in Production just building in advance and use only on one server uwsgi

CodePudding user response:

Could run under one server directly, I'll share other ways too.

Have your yarn build done and serve under Django's static files listen for js/css/image assets. Now about your routing, you need to capture frontend routes such a way that (when direct url is entered), Django responds back static assets itself.

The downside in above is that your APIs' urls have to be following some pattern which won't interfere frontend URIs (routes).

Running two servers CORS

Pack your react build with expressjs and serve it with some production grade server like pm2/ could use nginx static files too but to tackle routes issue (of React), you'll need to tweak nginx to listen to not only "/" but also other routes in frontend.

Now, calling django APIs, do enable CORS config to support calling APIs from your React site.

Can also use serverless to send your static files CORS behind the scenes.

If you're not having access to root of server, would need extra one to spare in former case. Otherwise to spin up a frontend backend process in same server machine won't do much weight.

Good luck!

  • Related