React web is not working properly after build (related axios api) - reactjs

I use
"webpack": "3.8.1" ,
"react": "^16.5.2"
when start to yarn start app is working
but after the yarn build and serve -s build, not to call api. (but react-router is working)
In other words, it does not work for the http request. After the build
But as a yarn start, http request runs well.
(I use proxy in package.json. front-end is react, backend is spring boot)

I suspect your issue is like this. When you are developing you are using a proxy setup in your package.json as you have stated in your question.
When you have this proxy setting, webpack dev server will proxy your request from the client to the server. This is what allows you to leave the baseurl off your request in the app. In other words, because of this proxy you can simply write /api/endpoint/.
When you build and serve using the serve module however, webpack dev server is no longer the one serving your app the the browser, which means there is no more proxying requests from client to server. This means you are making a request to just /api/endpoint/ which means there is no server actually getting your request.
Without actually changing your react code to use the full url including the base url in requests, you would need to actually have the server be responsible for serving the build folder to the web statically. By doing this, your /api/endpoint will point back the server that served the app which is also your api.

Related

Does npm server is necessary for ReactJS with django on production?

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
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!

React Github page not working as expected

I have create created a react app for ticket management and for connect with backend i have defined proxy in package.json as shown in the figure. this is finely working in local host. when i publish to github page, it not able to connect to backend and get 404 error since the proxy in package.json is not considering now. So how i define a proxy as like in the packag.json to github page i have hosted
github ripo: https://github.com/pranavmappoli/supportdesk
hosted page: https://pranavmappoli.github.io/supportdesk/
backend URL: https://pranavhelpdesk.herokuapp.com/
how could i resolve this issue other than putting the path in .env file
The webpack proxy is designed to be used as a hack during development.
In production you are supposed to configure CORS on the API or deploy the app to the same origin as it so you don't need a proxy.
An an alternative (not one I'd recommend) you could build a production ready proxy. If you do that then it will need to support CORS and cannot be hosted on Github pages which only support statics files.

Deploy ReactJS & ExpressJS to Nginx

I have a typical Ubuntu VPS setup (20.04LTS) where I have installed the Nginx Server.
I have two local repos with front-end reactJS code repo and back-end ExpressJS code repo.
I will start the ExpressJS on the 3000 port with forever start. I have also a mysql db.
I have configured Nginx to server the static files with the root /var/www/[Your repo name]/build; and I can open the html files and it is working.
The question is, do I need to start on another port for an example the ReactJS npm run start or is Nginx enough? Could you help me out with some links or best practices?
Thanks in advance.
If you're using CRA (create react app), it ships with a build script, so after running npm run build the whole app is built into static files including a index.html and some js and css files. all you need to do is to config nginx to serve that index.html. so nginx is enough for that. if you're using react-router in your app keep in mind that you may need to use try_files directive of nginx for serving that index.html for any incoming requests.
for more information about react-router and nginx see this.
If you're doing SSR (Server Side Rendering) you need a process manager like pm2 or forever to serve your app internally and proxy_pass directive of nginx to reverse proxy incoming requests to your app. More info
All the static files like index.html and assets will be served from your "root /var/www/[Your repo name]/build" folder, when the user opens your base url and sent as response for the get call. From your code, make sure to append '/api/' to all your backend requests from ui, so that it will get forwarded to your service running on port 3030.

create-react-app live reloading with proxy

We're developing a web app in React using create-react-app. The backend is written in Python and therefore we define a proxy in package.json to forward API calls to it during development as described here.
This, however, seems to interfere with the live reloading of the React development server when a source file is modified. In the Browser console we see:
The development server has disconnected.
Refresh the page if necessary.
And the backend server sees unexpected requests:
127.0.0.1 - - [22/May/2019 08:01:38] "GET /sockjs-node/227/inixucqn/websocket HTTP/1.1" 401 -
Is there any way to fix this? I guess we could configure the proxy manually, but this must be a problem everyone has when using proxy.

React.JS react-create-app backend same port

I am new to React.JS and using react-create-app to setup the project.
I am wondering is there a way to use the same host and port to response for API requests, (the server serves both front-end and back-end, like in Django).
The doc mentions about this but does not go into details.
By same host and port I mean I only need one terminal and run npm start once.
If there is only for development you can simply add
"proxy": "http://localhost:8000/"
to your package.json.
This will proxy your API queries from React to your other app working on another port (there 8000).
After you finish, you need to build production code (npm build command), which result is an index.html which loads builded js and css bundles.
From Django you need only point your IndexView to this file (you can do this as TemplateView, but maybe simpler is only render like there:
class IndexView(View):
def get(self, request):
index = open(str(settings.BASE_DIR.path('build/index.html')), 'r')
return HttpResponse(content=index.read())
Then only use your API from React - from this point both will work on common port.
Back to development mode - you can also configure your Webpack to build your app everytime you save changes and only run them from Django (or Rails, or Node, or whatever is your backend), but I prefer to use proxy, which keep both apps in their native contexts until you finish development. One disadventage of this solutions is that you need always run both apps simultaneously.
Some usefull info and soultions about this I found there: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/

Resources