React server side rendering - node.js

I’m trying to figure out how to render landing pages of my react js and node js site to be optimized for seo. Right now I use react for the front end and a proxy to access node api at a different port. Any suggestions on how I can render landing pages server side would be great

You can use Nextjs for SSR.
Next.js is a JavaScript framework created by Zeit. It lets you build
server-side rendering and static web applications using React.
Here are some cool features Next.js brings to the table taken from Learn Nextjs:
Server-rendered by default
Automatic code splitting for faster page loads
Simple client-side routing (page based)
Webpack-based dev environmet which supports Hot Module Replacement (HMR)
Able to implement with Express or any other Node.js HTTP server
Customizable with your own Babel and Webpack configurations

Related

How to make API calls server side REACT?

I am trying to render my react app server side for better SEO and better performance but since most of the content of the site is from an API and the API call is still happening on the client side the server is returning an almost black HTML template is there a way to make the API calls in server
P.S. I'm not using Redux
It is exactly what React as client and Express as server must do.
When you choose to develop your frontend by React and use 'npm build' to get output from your whole React project. you get only "ONE" index.html file. so you can't render multiple html pages by Express. you can only render the main html file and use api's to render other routes.
That's why you can't do such a thing.

is react supposed to be used on top of handlebars with node.js?

I want to use react on my node.js website. Im using express and handlebars with this website. Is react supposed to be used on top of all that? Or should i not use handlebars or express with react?
Also what is your opinion on node.js? Is it a declining technology? Is there something else that is better for me to use?
React is a view library. Is mean to be used to build your entire ui.
Then you have two options you can use it to build a SPA - single page application - and consume data through network request to your server or render multiple pages each of those will be a react app.
Node + express are backend tech that can help you to build the server side of a web app here you can create some api endpoints to return data to the server and comunícate with the database.
Node is a very good choice as is still and will be used for long time and many companies.

Is it possible to have an Apollo server, serve the React client for the same app?

I'm new to Apollo and JS on the server, but not new to React, GraphQL, etc. I'm trying to wrap my head around a clean way of having the server serve both the API, using Apollo, but also the client, which would be built with ReactJS and also Apollo.
I read the article on server-side rendering for Apollo. I might be interested in that in the future, but right now, I'm only interested in having a single project, deployed from a single server, instead of two.
Is it possible to do this cleanly? Is there a canonical way of doing it? can it use create-react-app or is that out of the question?
If you're not doing server-side rendering and just want to serve a Single Page Application (SPA) like what's built with CRA, you can do so using pretty much any HTTP framework or even without one. CRA just builds some static content for you, which you can serve using, for example, Express (see docs here). However, it's typically better to utilize Nginx or Apache for serving this content (at least in production). If you want to utilize Express or another HTTP framework to serve the files, you can utilize the appropriate integration for Apollo Server. If you're using Nginx, then you can stick with the standalone library, assuming you don't want to expose any additional routes on your HTTP server.
In development, you don't want to have to constantly manually rebuild your React application when you make changes, so CRA actually runs a server for you that serves the app and enables hot reloading. Because this is a separate server from your API server, you'll typically want to enable proxying your requests to the API.
It's possible, the setup for apollo is the same as any server, for example, you can use express to serve static files and implement apollo with some minor changes see the docs for apollo server express and implement the one that you are familiar with

Why use Express with ReactJS

I'm currently working on a new ReactJS application. In all the previous applications I've built I used Express for the server side rendering. I used Express because routing in production mode wouldn't work if I didn't.
So I've recently discovered that it's posible to just always redirect my React app to my index.html file and let the React routing just do it's work. This also works when I deploy my application to production.
I know Express can be usefull for SEO porposes but other than that I have no idea why I would need to use it. So am I just missing something? Or is it just fine to don't use Express with my React application if I don't need any SEO.
React configured as a Single Page App, renders the express routing all but unnecessary. The standard transition from a web server rendered app to a single page app is to change the server into a REST Web API and any server side data needed from the React App is obtained through AJAX calls.
So in most cases you don't need Express when you are using React to handle your routing except for in less common cases when you might want to use express as a reverse proxy or something.
If you want to do server-side rendering, you'll need a node server (typically but doesn't have to be express) to render your React components. This could not only help with SEO but also allow your page to appear to load faster (you wouldn't have to wait for the JS to download and run before the user sees the HTML).
If you don't care about the benefits, don't use SSR.
when deploying react application it is just usually an html and a JS file. So you need some kind of server to host it. Luckily there are services out there that offers that like S3, Github etc.
You're using Express because it can host the html and js files. So technically you don't need specifically express but you need some server that will host your files.

Does using React.js limits us on using a node.js server

I have been working on React.js for a month now. I have been using Webpack dev server which is a node.js Express server and enables us to render react.js on browser. I want to know whether using React.js will limit us on using only a node.js http server or is there a way we can use a simple http server as well with React.js.
I have also been wondering whether it is useful to use React.js for developing webpages that have mostly pre-fixed contents text fields, data etc. during request/response for any API operation.
Does using React.js limits us on using a node.js server
No. React is predominantly a client side library. It doesn't care how it is served to the client.
In the end you are just writing JavaScript. You can deliver the JavaScript code to the client whichever way you want.
It depends. Generally the answer is no. If you only want to use it as a client-side library, it doesn't matter what the web server is. But actually, react components can also be rendered on server side so you can develop universal/isomorphic apps. In that case you will need a node.js server.
Take a look at this universal example.
we are using php+yii2+react +redux+es6 without node.js server, but ngnix. At the end it is just javascript, so that we use gulp + babelify to translate es6 to es5 and then php yii2 application renders basic container to which react application renders himself.
Node.js is one of the options, you can use wathever server you want, just need something like gulp or webpack to compile your js and all dependencies to the ready to use standalone js.

Resources