Connect MongoDB to an interface?

I created a database with Node.js and MongoDB , and I am writing an Angular.js application that should call records from my database, as well as the ability to write to these records.

I know that there are some security issues when writing directly from javascript to the database, but I'm completely new to this. What more, I cannot find any instructions on how to send data from MongoDB to my interface so that I can use it!

How to tie them together? In Node.js I used the javascript require function to load into and read from / write to my database, but I cannot figure out how to do this in the browser. In node I used the mongojs module to connect the two together, but this does not work in my Angular application, since I cannot use require .

The main question: how to load into MongoDB in an interface?

EDIT: I think this is a simpler question about calling MongoDB for an interface rather than an angular-specific one. If I am wrong, let me know.

+7
javascript mongodb
source share
1 answer

Short answer: You do not.

Long answer: MongoDB is not intended to send data directly to the client. The client interacts with the application on the web server - in your case, implemented in Node.js - and the web server communicates with the database.

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

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

The client application does not understand that the server used the database to execute the request.

My training recommendation for you is to leave the database for now. Use angular.js to have the client load some static data from the Node.js server application. When you get this working, expand the server side to get data from MongoDB.

+11
source share

All Articles