将MongoDB连接到前端? [英] Connecting MongoDB to the front-end?

查看:491
本文介绍了将MongoDB连接到前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Node.js MongoDB 构建了一个数据库,我正在编写 Angular.js 应该从我的数据库调用条目的应用程序,以及能够写入这些条目的应用程序。

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.

我知道直接从javascript写入数据库存在一些安全问题,但我对这类事情完全不熟悉。更何况,我找不到有关如何将数据从 MongoDB 发送到我的前端的任何说明,以便我可以实际使用它!

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!

如何将两者捆绑在一起?在 Node.js 中,我使用了一个javascript require 函数来加载我的数据库并从中读取/写入,但我无法找到在浏览器中执行此操作的方法。在节点我使用 mongojs 模块将两者连接在一起,但这在我的Angular应用程序中不起作用因为我不能使用 require

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.

主要问题是:如何在MongoDB中加载到前端?

编辑:我认为这是一个关于将MongoDB调用到前端而不是特定于角度的更基本的问题。如果我错了,请告诉我。

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.

推荐答案

简答:你没有。

长答案: MongoDB不是为了直接向客户端公开数据而设计的。客户端与Web服务器上的应用程序交互 - 在您的情况下在Node.js中实现 - 并且Web服务器与数据库进行通信。

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 | 
|        ------->        |    |         |    
|         |    |         |    |         |
|         |    |        ------->        |
|         |    |         |    |         |
|         |    |         |    |         |
|         |    |        <-------        |    
|         |    |         |    |         |   
|        <-------        |    |         | 
|         |    |         |    |         |  
+---------+    +---------+    +---------+  

这意味着在实践中,在用户浏览器中执行的javascript客户端应用程序向Node.js发送请求(通过普通页面请求,通过Websockets通过XmlHttpRequest或其他一些方法)。 Node.js接受此请求,然后联系MongoDB以获取实现它所需的数据。当Node.js从数据库接收数据时,它使用它来构建响应,并将其发送到客户端。

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.

我的学习建议是暂时退出数据库。使用angular.js使客户端从Node.js服务器应用程序加载一些静态数据。当你使用它时,扩展服务器端以从MongoDB获取数据。

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.

这篇关于将MongoDB连接到前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆