2011-11-24 60 views
1

我使用Nathan Broslawsky的簡單MVC結構。我有下面這些代碼。NodeJS在回調延遲時無法對瀏覽器進行response.write

ArticleProviderDBController.prototype.Show = function(data) { 
//Init Model 
var res = this.Response; 
var model = this.getModel(); 
var view = this.getView("ArticleProviderDB"); 

model.findAll(function(error, article_collections){ 
     if(error) console.log(error); 
     view.renderGH(res, data, article_collections); //this will actually call the renderGH function to serve a html file with data from DB but it is not working. 
     res.write('inside callback'); //this will not. 
     //res.end(); 
}); 
//console.log(_self.Response); 
res.write('outside callback'); //this will be shown on my browser. 
//res.end(); 

}

其實我試圖跟人做什麼用expressjs

app.get('/', function(req, res){ 
articleProvider.findAll(function(error,docs){ 
    res.render('index.jade', { 
     locals: { 
      title: 'Blog', 
      articles:docs 
     } 
    }); 
}) 

});

但似乎它不工作。

我還看到一篇文章NodeJS response.write not working within callback最近發佈,但他的解決方案不適合我。我的主要目標是使用Nodejs創建的簡單MVC結構,而不使用其他模板(如expressjs)來爲數據庫查詢提供html。謝謝。

回答

0

我沒能找到上述問題的解決方案,但我發現了一個非常好的由davidpirek創建的nodejs mvc框架示例。給出的例子是工作,非常好。 我做了一些小的改動,以便它可以在我的開發窗口上運行。

http://www.nodejsmvc.com/

非常感謝davidpirek。