【问题标题】:FeathersJS: Cannot add routes after initializing appFeathersJS:初始化应用程序后无法添加路由
【发布时间】:2016-07-07 13:35:51
【问题描述】:

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

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

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 秒,就像那里定义的任何路由一样。我已经通过指南寻找我误解的根源。但我有点卡住了。

【问题讨论】:

    标签: feathersjs


    【解决方案1】:

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

    【讨论】:

    • 为什么会这样?它清楚地回答了 OP 的问题?
    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 2011-03-06
    • 2015-06-09
    • 1970-01-01
    • 2018-02-17
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多