Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...