Reputation: 81
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.
Upvotes: 7
Views: 4020
Reputation: 230286
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.
Upvotes: 3
Reputation: 1
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.
Upvotes: 0
Reputation: 370639
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.
Upvotes: 2