1

新手 FeathersJS 用户在这里。我显然缺少一些关键的理解。

我正在尝试使用 MySQL 模型创建一个简单的 REST API。我正在尝试遵循此问题线程中文档引用的代码结构。我在初始app.use()块工作中定义的路线,但不是在它之后定义的路线。部分代码在这里,休息在这个要点

const app = feathers();
app.configure(configuration(path.join(__dirname, '..')));

app.use(compress())
  .options('*', cors())
  .use(cors())
  .use(favicon(path.join(app.get('public'), 'favicon.ico')))
  /* THIS ROUTE WORKS FINE */
  .use('/', serveStatic(app.get('public')))
  .use(bodyParser.json())
  .use(bodyParser.urlencoded({
    extended: true
  }))
  .configure(hooks())
  .configure(rest())
  .configure(socketio())
  .configure(models)
  .configure(services)
  .configure(middleware);

const appModels = app.get('models');
const beerOptions = {
  Model: appModels.beer,
  paginate: {
    default: 15,
    max: 50
  }
};

/* NEITHER OF THESE ROUTES WORK */
app.use('/beer', service(beerOptions));
// IF YOU DELETE THE DEFINITION ABOVE AND UNCOMMENT 
// THIS NEXT LINE, THE ROOT URL GIVES A 404
// app.use('/', serveStatic(app.get('public')));

应用程序时我没有收到任何错误npm start。但是,我的/beer路线和那里定义的任何路线一样只有 404 秒。我已经通过指南寻找我误解的根源。但我有点卡住了。

4

1 回答 1

2

就像在 Express 中一样,中间件的顺序(另外对于 Feathers,configure调用)很重要。对于生成的应用程序,.configure(middleware); 必须在其他所有内容之后最后运行,因为它注册了一个notFound将引发 404 错误的处理程序。之后的任何中间件(错误处理程序除外)将永远不会运行。

于 2016-07-07T05:35:51.420 回答