【发布时间】:2016-07-17 00:04:24
【问题描述】:
以下问题: 我正在运行一个 Nodejs 服务器和一个 mongoDB。到目前为止,我一直将查询结果作为 json-data 发送到浏览器。
app.get('/gesamtergebnis', function(req,res){
User.aggregate([{$group: {
_id: "$Art",
Anzahl: {$sum: "$Anzahl"}
}
}], function(err,docs){
if(err){console.log(err);}
else {res.json(docs);}
});
});
我的结果是 - 这些 json 响应的性质是什么 - 格式不正确。所以我建立了一个带有表格等的html文件。和一个小 ng-controller 用 ng-repeat 获取表中的数据 - 没问题 到目前为止。
function AppCtrl($scope, $http) {
console.log("Hello from AppCtrl")
var showTotal = function(){
$http.get('./gesamtergebnis').success(function(response){
console.log("I got the data I requested!");
$scope.gesamtergebnis = response;
});
}
}
我的路线是:
app.use(express.static(__dirname + "/public")); In the public folder is my index.html
我的问题是:index.html 没有收到任何服务器响应;
我需要
- 将请求路由到某个 html(res.sendfile 没有帮助,因为我无法将 json 数据添加到响应中)和
- 在同一条路由命令中从 mongodb 请求中发送 json 数据
有没有办法将服务器响应重定向到带有数据的某个 html(还有其他 htmls 然后索引)端点?
解决此问题的最佳方法是什么?使用 Angular 在客户端完成路由?我怎样才能轻松做到这一点?
任何帮助都会很棒!
胡乔
【问题讨论】:
-
这是为您调试响应还是希望您的 api 按设计在 html 上显示 json?
标签: angularjs json node.js express routing