【发布时间】:2017-09-07 03:43:15
【问题描述】:
非常困惑,不确定我是否正确地考虑它。请让我知道我还需要展示什么才有意义...
我在猫鼬中有两个不同的功能。一个索引所有列表,另一个索引唯一用户列表,并在我的猫鼬函数中进行过滤。但是,在我的路线中,如果 $http url 相同,我如何在我的个人资料页面上点击唯一功能?
//From server.js to controllers
app.get('/api/listings', controllers.listings.index);
//all listings shown on home page
function index (req, res) {
db.Listing.find({}, function(err, allListings) {
res.json(allListings);
});
}
//From server.js to controllers
app.get('/api/listings/', auth.ensureAuthenticated, controllers.profiles.indexUnique);
//unique function that I want to show on profile page only
function indexUnique(req, res) {
db.User.findById(req.user, function (err, user) {
if (err) {console.log(err);}
db.Listing.find({uid: req.user}, function(err, listings) {
if(err) { console.log('Error', err); }
res.json(listings);
});
})
}
如果 url 路由相同,我如何调整我的 http 请求以达到我的独特功能?
// Angular http request in my home controller
$http({
method: 'GET',
url: '/api/listings'
}).then(function successCallback(response) {
vm.listings = response.data;
}, function errorCallback(response) {
console.log('Error getting data', response);
});
【问题讨论】:
-
我建议您使用唯一参数,即 /api/listings/:listingId。然后你可以发送带有参数listingId的请求。这是restfull方式并且易于理解。我不明白你为什么要使用相同的网址。
-
你也可以使用这个库。这对于以有意义的方式使用 api 非常有用:github.com/mgonto/restangular
标签: javascript angularjs node.js mongodb