当回调有延迟时,NodeJS无法响应。浏览器

时间:2011-11-24 16:56:21

标签: model-view-controller mongodb node.js

我正在使用Nathan Broslawsky的简单MVC结构。我在下面有这些代码。

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。谢谢。

1 个答案:

答案 0 :(得分:0)

我找不到上述问题的解决方案,但我找到了一个由davidpirek创建的非常好的nodejs mvc框架示例。给出的例子很有效。 我做了一些小的改动,所以它可以在Windows上运行我的开发。

http://www.nodejsmvc.com/

非常感谢davidpirek。