Connect-Rest 不处理请求

问题描述 投票:0回答: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));

当我向

http://localhost:3000/api/posts
发出获取请求时,我收到此信息消息

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

然后是一些关于找不到视图的错误,因为我还没有发表任何视图。我怎样才能让 connect-rest 来处理这些请求?

node.js express node.js-connect connect-rest
1个回答
1
投票

我遇到了同样的问题,并通过将行

app.use(rest.rester(apiOptions));
移动到定义第一个API方法的行上方(第一次使用
rest.get(...)
)来解决它。

© www.soinside.com 2019 - 2024. All rights reserved.