MySQL 在 AngularJS 中使用数据库 - 我应该在哪里编写数据库连接代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19758213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using database in AngularJS - where should I write DB connection code?
提问by Blaszard
I'm now building up a web app using Node.js, AngularJS and either MySQL or MongoDB. However, when I tried to use AngularJS with a controller which includes datasets fetched from database, I wonder where I should write the code in...
我现在正在使用 Node.js、AngularJS 和 MySQL 或 MongoDB 构建一个 Web 应用程序。但是,当我尝试将 AngularJS 与包含从数据库获取的数据集的控制器一起使用时,我想知道应该在哪里编写代码......
I'm now writing in the following code (search.ejs, not including full portion (such as html
tag)):
我现在正在编写以下代码(search.ejs,不包括完整部分(例如html
标签)):
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="/javascripts/searchController.js"></script>
<div ng-app class="row" ng-controller="searchCtrl">
<input ng-model="query">
<ul class="search">
<li ng-repeat="i in list" | filter:query">
<a href="{{i.url}}">{{i.name}}</a>
</li>
</ul>
</div>
And I want to fetch data in list
from database and use it. So here's searchController.js
file:
我想list
从数据库中获取数据并使用它。所以这里的searchController.js
文件:
function searchCtrl($scope){
$scope.list = [
{
'name': 'Michael',
'url': 'mic'
},
{
'name': 'Bob',
'url': 'bob'
}
]
}
However, what I want to do is instead of writing out data in the $scope.list
variable manually, use data in database of either MySQL or MongoDB. (And MySQL is my preferred language, but MongoDB seems like the better in this case, I think.) So how can I connect to DB?
但是,我想要做的不是$scope.list
手动写出变量中的数据,而是使用 MySQL 或 MongoDB 数据库中的数据。(而且 MySQL 是我的首选语言,但我认为在这种情况下 MongoDB 似乎更好。)那么我如何连接到数据库?
I also have one file called search.js
, which is the following one:
我还有一个名为 的文件search.js
,如下所示:
exports.index = function(req, res) {
res.render("search", {
}, function(err, res){
res.render("index", {
content: res
});
});
}
And finally, I also want to hear from you whether I should first save data from database to any file and use it by opening and closing the file, or I should directly connect to database whenever the request comes to fetch data, in terms of performance and security issues, (the data in database would be updated every day, so I have to run a script to automatically recreate the served file anyway).
最后,我还想听听您的意见,我是应该先将数据库中的数据保存到任何文件中,然后通过打开和关闭文件来使用它,还是应该在请求来获取数据时直接连接到数据库,就性能而言和安全问题,(数据库中的数据每天都会更新,所以我必须运行一个脚本来自动重新创建所提供的文件)。
Thanks.
谢谢。
回答by surui
I believe the best practice is to have an http route that you can get the list from. Let's assume it's a list of articles, then you:
我相信最好的做法是拥有一个可以从中获取列表的 http 路由。让我们假设它是一个文章列表,那么你:
- have a route in your web server from which you could :
GET /articles
(you could easily implement it using mongodb drivercollection
&find
functions) - on your angular controller :
- 在您的 Web 服务器中有一个路由,您可以从中 :(
GET /articles
您可以使用 mongodb 驱动程序collection
和find
函数轻松实现它) - 在您的角度控制器上:
(client code)
(客户代码)
function searchCtrl($scope, $http){
$http.get("/articles").success(function(articles, status, headers, config) {
$scope.articles = articles
}
}
as for your second question, you could render the list from the server, but if you want to update the list, you will need to use $http
regardless. moreover, note that angular templates use {{}}
, so you might override them if you're not careful - that's why I think it's not a good practice.
if, however, you had some configuration you want to inject from your web server, then you could inject a code similar to this (as a script):
至于您的第二个问题,您可以从服务器呈现列表,但是如果您想更新列表,则$http
无论如何都需要使用。此外,请注意 angular 模板使用{{}}
,因此如果您不小心,您可能会覆盖它们 - 这就是为什么我认为这不是一个好习惯。但是,如果您想从 Web 服务器注入一些配置,那么您可以注入与此类似的代码(作为脚本):
angular.module("myModule.configuration", []).constant('myConfiguration', {siteName:"http://www.my-site.com");
and then you could inject 'myConfiguration' to all your controllers (don't forget to add "myModule.configuration"
to the dependencies array)
然后您可以将“myConfiguration”注入所有控制器(不要忘记添加"myModule.configuration"
到依赖项数组)