7

A friend just asked me a question: Can I make a React app and connect to Mongodb without setting up an Express server? I couldn't answer the question, I'm used to using the frontend-backend-database flow but I can't think of a reason why it's not possible to skip the backend part. I'm not sure if I can install mongoose and interact with the database directly from React, but it seems reasonable to me, especially for simple apps that just fetch and insert some info from and into a database. I think it might not be a great practice because it requires extra computational work on the browser, is that right? Sorry if the question is silly, I'm new to the web dev world.

0

3 Answers 3

2

The database needs to exist somewhere.

If only an individual client on a single browser needs to be able to access their data, this can be achieved with Local Storage or IndexedDB, which can be used with React. This way, the data is stored inside the client's browser data.

because it requires extra computational work on the browser, is that right?

There's nothing at all wrong with that, as long as you aren't storing and processing large amounts of data, which would sometimes be a problem.

But for data that needs to be shared between different users, or persistent across different logins on different machines, putting the "database" on only the front-end won't work - you will need to put the database on the backend.

So if such a persistent database is needed, it will need to be on the back-end - and if you don't set up back-end logic in addition in order to interface with the back-end database, the database will essentially be freely open to manipulation by clients, which is quite a bad idea.

2
  • Does data that needs to be shared between different users need a database on the backend, or can the app send the data from user to user via text or email?
    – Joe
    Aug 1 at 2:51
  • 1
    A distributed browser-based database would be theoretically possible, but it would be quite difficult to implement reliably, and trust would be a serious issue. That's different from clients manually sharing data with text and email, which would work, but would involve much more manually handling of data than one would expect nowadays. Aug 1 at 3:00
2

Can I make a React app and connect to Mongodb without setting up an Express server?

You probably can, but you shouldn't (unless it is only you who is ever going to use this app). If the database is exposed directly to the frontend, anybody can see and change anything, including wiping the DB.

0

It's totally ok to skip the backend when building with react, you don't necessarily have to build an api for your react App using expressJs. However, you do need to decide which database to use - noSequel Dbs like MongoDb and Firestore are best suited for this use-case. You definitely will be needing a robust state management library like Redux/ReduxToolkit. With these in-place, you could setup a two-way system for efficient and managed flow of data between your App's state and Db, and you can update (sync) your frontend from State based on certain events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.