我在客户端有这个:
var Article = Backbone.Model.extend({
urlRoot: '/wiki'
});
var ArticleView = Backbone.View.extend({
render: function(){
var article = new Article({term:'test'});
article.fetch(); //sends a request to wiki, not to wiki/:term as i want to
}
});
,这在服务器(节点)上:
//Never gets hit
app.get('/wiki/:term', function(req, res){
console.log('/wiki/term');
});
app.get('/wiki', function(req, res){
console.log('/wiki');
console.log(req.params.term); //produces 'undefined'
});
我需要做什么才能从正确的路由(即wiki /:term)中获取骨干,并实际将term参数发送到服务器?
编辑:
我能够使用req.query.term
检索服务器端的请求参数,而不是req.params.term
答案 0 :(得分:1)
var Article = Backbone.Model.extend({
urlRoot: '/wiki',
url: function () {
return this.urlRoot + "/" + encodeURIComponent(this.get("term"));
}
});