Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting MongoDB to the front-end?

I've built a database with Node.js and MongoDB, and I'm writing an Angular.js app that should call in entries from my database, as well as being able to write in those entries.

I'm aware that there are some security issues with writing directly from javascript to a database, but I'm completely new to this sort of thing. What's more, I can't find any instructions on how to send data from MongoDB to my front-end so that I can actually use it!

How do I tie the two together? In Node.js, I was using a javascript require function to load in my database and read/write from it, but I can't figure out a way to do this in the browser. In node I was using the mongojsmodule to connect the two together, but this doesn't work in my Angular app as I can't use require.

The main question is: How do I load in MongoDB to the front-end?

EDIT: I think this is a more basic question about calling MongoDB to the front-end, rather than angular-specific. If I'm wrong, let me know.

like image 404
JVG Avatar asked Jul 04 '13 11:07

JVG


People also ask

How does MongoDB connect to front end?

Setting Up MongoGo to the MongoDB Atlas website, click “Start Free”, and create an account. Once you are inside the dashboard looking page, create a new project (name it anything you want). Create a new cluster. Leave Global Cloud Configuration with its default parameters.

How do I access MongoDB in my browser?

By default, MongoDB starts at port 27017. But you can access it in a web browser not at that port, rather, at a port number 1000 more than the port at which MongoDB is started. So if you point your browser to http://localhost:28017, you can see MongoDB web interface.


1 Answers

Short answer: You don't.

Long answer: MongoDB isn't designed to expose data directly to the client. The client interacts with the application on the webserver - in your case implemented in Node.js - and the webserver communicates with the database.

+---------+    +---------+    +---------+ 
|Browser  |    | Node.js |    | MongoDB | 
|        ------->        |    |         |    
|         |    |         |    |         |
|         |    |        ------->        |
|         |    |         |    |         |
|         |    |         |    |         |
|         |    |        <-------        |    
|         |    |         |    |         |   
|        <-------        |    |         | 
|         |    |         |    |         |  
+---------+    +---------+    +---------+  

This means in practice, that the javascript client application executed in the users browser sends a request to Node.js (via a normal page request, via XmlHttpRequest via Websockets or some other method). Node.js accepts this request and then contacts MongoDB for the data required to fulfill it. When Node.js received the data from the database, it uses it to build the response, and sends it to the client.

The client application doesn't realize that the server used a backend database to fulfill the request.

My learning recommendation for you is to leave the database out for now. Use angular.js to make the client load some static data from the Node.js server application. When you got this working, extend the server-side to obtain the data from MongoDB.

like image 108
Philipp Avatar answered Sep 21 '22 17:09

Philipp