【问题标题】:Connect-Rest not handling requestsConnect-Rest 不处理请求
【发布时间】:2014-08-26 20:55:41
【问题描述】:

我的 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 rest express connect


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 2014-07-06
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2017-12-29
      • 2018-12-04
      相关资源
      最近更新 更多