How to create a react-redux app with node server as backend? - javascript

I am trying to create a react-redux app using node server as backend. Is it possible to make the node server serve the react-redux app instead of running react-redux using dev server a=in one port and node on another port?
Need some idea to start with. Thanks in advance:)

Yes.. it is
you can serve you react app on '/'
and listen for API request in another route
so you don't have separate codebase for the react app and the api backend code
You can use express to serve the react app on a particular route
i.e my-app.com/
then serve backend related content on another route
i.e
my-app.com/api
so when a request is made to my-app.com/ express serves express serves backend resource or API

There are a few steps I take when creating an express/react app together. I'll create a server and a client directory. The client dir is created with create-react-app and the server can be created via express-generator for example. My project dir (the one that contains both of them) is basically just glue that melds the two together. In the client app, I'll add proxy:localhost:3001 (or whatever port your express api is running) and I use concurrently to run both servers (client and server - as client is being run by webpack-dev-server) at the same time. They run as separate servers during development, but when I make an api call, it's as if I'm making it directly to the express server itself.
The only other thing to worry about is deploying the application. You can use the build command that comes with create-react-app and copy that over to the public directory in your express application that is served up via express.static.
Here's a quick example to take a look at:
https://github.com/overthemike/heroku-skeleton

This is an useful doc for redux SSR setup. This helps avoiding running client and server at two ports.
Redux SSR

Related

Deploying a node.js frontend and spring boot backend as a self contained app locally

I have a web app that has a separate spring boot backend and a nodejs frontend.
is it possible to make them both run on the same bundle, like building a singe executable? or do i need a stand alone service for each? Both are having their own jenkins pipelines and git repositories.
My backend server is just a API that provides the frontend with simple data via REST services. If it is possible what would be the way to do it?
A nodejs is a backend technology, not frontend.
You can use a nodejs as an entry point to a spring boot application but it is a code running on the server side. So nodejs is not just a list of static files as it happens for a real front end developed in react, angular or vue (or vanilla javascript). It has also an engine to run the code.
So is not possible to run nodejs server using files that are compressed in the executable jar. Instead is possible to put a real front end application (react, angular...) adding static files in the directory /resources/static
It was maybe a little missleading. My frontend is an app runing on node.js server and may backend is a java app runing on spring boot server. They are communicating on localhost via Rest.
I want to create a single entry point that starts the node.js server (I call this unit frontend) and the spring boot server ("backend") at the same time. So it feels like a self contained system.Currently the spring boot app is delivered as windows executable.

How to connect NodeJS backend with NextJS, how to set proxy?

I am developing Social media app using NextJS & Node. I have created seperate backend using NodeJS. Now I want to connect NextJS frontend with Node backend. but when i set the "proxy": "http://localhost:8080/" inside package.json file that is inside my nextjs app. but still it's not fetching the API.
Based on my understanding, I don't think Next.js use proxy in package.json. If you want to achieve how proxy works in Create-React-App, you need to assign http://localhost:8080/ to an environmental variable in next.config.js.
You can access your base URL from when you try to make HTTP call like fetch(`${DEV_URL_HOST}/users`)
References
Proxying API Requests in Development

Struggling to understand how React determines address of API server

I have a project with a client folder containing a React app bootstrapped with create-react-app. I also have a server folder with an express API server running on localhost:80.
Originally, I ran my frontend and backend as separate servers, making requests to the API server by making requests with fetch("url") and using "proxy": "localhost:80" in my package.json
I have recently altered the project so that my server serves the static frontend files like so: app.use(express.static(path.resolve(__dirname, "../client/build")))
I then tested running the server on two different ports 80 and 3000, and the frontend and backend both worked perfectly but I believed it would only work when the server was run on port 80
How does my frontend now know where to call the API server when it is running on different ports?
Proxy is used in the development environment and after that when you make build then its convert into the static HTML, CSS and JS files and that can be run in any port using node if you try that in a different port than it will also work as same
Proxy is not made for the production environment
https://github.com/facebook/create-react-app/issues/1087

How to start node.js server on Azure Web App service?

I have a create-my-react bootstrapped application that is essentially a website that uses some FETCH API calls to a external API and it is deployed and works fine.
However, I added my own Nodejs backend, by creating a server and using using express for the routes/middleware. Everything works fine locally. I can hit my internal API endpoints (localhost:3000/myapiurlhere) and it performs an action on a database.
I have to run npm start to start up the create-my-react-app locally and then manually run the node server by node src/server.js then my internal API works.
The Azure Web App service is basically a preconfigured server with the Node RUNTIME on it, and it only seems to give you access to the D:\home\site\wwwroot folder (Windows server).
Do I need to find a way to run node server.js command on the server to start my node backend, or should it be running automatically? Also, I'm using create-my-react-app and npm run build , so it creates a build folder with a nested static folder.
I have started up REST APIs on Java on my Linux Ubuntu servers before but never on an App Service like Azure. How can I achieve what I'm trying to do?
Here is my server.js file:
const http = require('http');
const app = require('./app');
const port = process.env.PORT || 3001;
const server = http.createServer(app);
server.listen(port);
You dont have to do anything special, Have you followed this page on how to deploy basic nodejs app on Azure AppService?
One additional thing you need to do is that pass the Node version on appsettings of the appservice.
WEBSITE_NODE_DEFAULT_VERSION for the setting key.

Proxy in production React for fetch API express

I was developping a app with React app. In developing env i was using proxy but I'm deploying the app and I saw that proxy didn't work in.
I read about http-proxy-middleware. It can be a solution or it don't works too?
Any way to do this without config the server with redirects to other port?
I need to continue fetching to my API server.
The best way what I found without configure server and NGINX is follow this steps:
Build front
Move folder into a backend server.
Put that code after routes:
if (process.env.NODE_ENV === 'production') {
app.use(express.static(`${__dirname}/yourFrontFolder/build`));
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/yourFrontFolder/build/index.html`);
})
...
And build your backend code and access to your backend port like frontend.
You don't usually need a proxy in your React app when it is deployed. To deploy, you usually run npm run build, which creates a build directory containing all the compiled JavaScript and HTML files you need for the deployment. These are then served by a web server, such as NGINX or by your backend application.

Categories

Resources