2014-06-29 63 views
1

我有这样的代码在我的app.js文件来处理API请求连接休息不处理请求

app.use('/api', cors()); 
rest.get('/posts', function(req, content, cb) { 
    Post.find(function(err, posts) { 
    if (err) return cb({error: 'Internal error.'}); 
    cb(null, posts.map(function(p) { 
     return { 
     title: p.title 
     }; 
    })); 
    }); 
}); 

var apiOptions = { 
    context: '/api', 
    domain: require('domain').create(), 
}; 

apiOptions.domain.on('error', function(err){ 
    console.log('API domain error.\n', err.stack); 
    setTimeout(function(){ 
    console.log('Server shutting down after API domain error.'); 
    process.exit(1); 
    }, 5000); 
    server.close(); 
}); 

app.use(rest.rester(apiOptions)); 

当我做一个GET请求来http://localhost:3000/api/posts我得到这个信息消息

info: Request won't be handled by connect-rest 

之后是关于找不到视图的一些错误,因为我还没有发表任何意见。我如何获得连接休息来处理这些请求?

回答

1

我遇到了同样的问题,并通过将app.use(rest.rester(apiOptions));行移至第一个API方法定义的行上方(第一次使用rest.get(...))来解决此问题。