【问题标题】:HTTP endpoint not being called, endpoint with path param called instead?没有调用 HTTP 端点,而是调用了带有路径参数的端点?
【发布时间】:2016-09-13 11:44:57
【问题描述】:

当我向http://localhost:4101/endpoint/field 发出请求时,endpoint/:id 始终会被记录。

为什么没有endpoint/field 登录?

我知道:id 是一个路径参数,可以是任何东西,但我明确表示field 应该以不同方式处理。

'use strict';
var express = require('express');
var app = express();
var PORT = 4101;

app.route('/endpoint/:id')
  .get(function(req, res) {
    console.log('endpoint/:id');
  });
app.route('/endpoint/field')
  .get(function(req, res) {
    console.log('endpoint/field');
  });

app.listen(PORT, function(err) {
  if (err) {
    console.log('err on startup ' + err);
    return;
  }

  console.log('Server listening on port ' + PORT);
});

【问题讨论】:

    标签: node.js express routes


    【解决方案1】:

    路线的顺序很重要。使用第一条有效路线。

    /endpoint/:id/endpoint/field 有效,因为 :id 可以是任何东西

    所以你需要切换顺序。

    另见Node.js Express route naming and ordering: how is precedence determined?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多