我在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));
当我向http://localhost:3000/api/posts
发出获取请求时,我收到此信息消息
info: Request won't be handled by connect-rest
之后是一些关于没有找到观点的错误,因为我还没有提出任何意见。如何让connect-rest处理这些请求?
答案 0 :(得分:1)
我遇到了同样的问题并通过将行app.use(rest.rester(apiOptions));
移到定义第一个API方法的行(第一次使用rest.get(...)
)之上来解决它。