Why do we need node-js if we can do routing and fetching with all react for making a SPA?

Sorry for this dumb question,iam really a beginner…
If we can do routing and fetching all by react.js which is indeed a front-end library then why do we need nodejs?

Hi @asadx,

Generally, when building a web app, or an app in general, you want to ensure that it runs smoothly for the user with as little lagging as possible. In order to do that, we need to do as much backend work for them as possible. If we are calling everything directly to and form the front-end, we are putting alot of strain on the user side of the website which will cause lagging and could prevent the user from using the service. The average length of attention these days is mere seconds, so if our websites take longer to load, chances are it will deter people.
Node.js takes that pressure off the React front-end. By doing all of the heavy sorting and calls in the backend, we free up the front end to run quicker, more efficiently. The fetch and routing in the front end is necessary to pull the data from Node.js though, but after it has been sorted. This is more important as you delve further into using servers of your own, rather than just calling from API’s from other sources.
I hope that helped clarify some things for you :slight_smile:

If you are building a front-end only website then you don’t need node as it is used on the backend. But if you are building a full-stack website that will persist data in a database then you can use node (or several other options) to build a web server which will save data to your database. So for example, in a social media website, node could ensure that the comments were saved in a database so that your front-end can get them on another visit to the site.

1 Like

Let me take a scenario,

  1. if i make a simple web app with no server side.
  2. if i make a simple web app with server side.

which would be more more efficient then?

this make sense,

so you are saying,if there are some’ persistant data’ then we have to use…for that we need backend.
elsewise for simple web app,front end can do the work?

then you do not need to have node. Node is specifically if you are going to have a backend to your app. It is like the other response to your question said:

What you are saying in this first scenario is that you just want a front end app with no database to pull from.

then you need Node. Node will be necessary for any backend work which also is server-side. Database has to be on a server so they go hand in hand. Any information you want to pull from or store, needs a database. So log-ins, comments, anything you want to stay on the page if it has been manipulated by a user, needs to be saved to a database.
If you are just displaying information that is hard-coded into the visible side of the site and the user also cannot change anything for data, then you have no need for a backend and therefore no need for Node.

1 Like

Yes, if you need to persist data then you can do that in a database (such as PostgreSQL, MySQL, or MongoDB). Node can be used for the server that would manage this. A node JS framework such as Express makes it a lot easier to build the server. But many other languages can be used for the web-server such as Python, PHP or Ruby. It is just pretty convenient to be able to use the same language (JS) that is used in React if you are using that already. So if you have a e-commerce site for example, you can save all contact info, order info and other data that needs to be persisted in the database. A stack such as PERN (PostgreSQL, Express, React, Node) can be used to create this.

Another reason to use node for a back-end web-server would be to keep information secure from the front-end user since front-end code runs in their web-browser. Users can basically get access to front-end code through the dev console. So if you are building an app that needs to keep some data secure then you typically also store this in the back-end. For example, an online test.

1 Like

Some good points are already made. I would like to clarify one two small things

We can only store a little/limited amount of data in the front-end.

If you have an authentication system (login, register), we need back-end to store the data, so a user can’t access someone else his password (@mike7127143742 covers this), but we do also need to store a token in the front-end. So the back-end knows which authenticated user is making the request.

there are different techniques (cookies, local storage, caching) to achieve this. Storing in the front-end is sometimes desired.

its also important to distinguish between front-end and back-end. Even when you have a website made only in html, css (with optional JS or JS libraries like react), you will still need a server to host your website

then users can make requests to your website/server, and your website will return the html, css and JS which is then rendered by the browser


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.