错误:未捕获(在承诺中):错误:无法匹配任何路由。网址细分:'executions / 190'

时间:2018-04-11 14:25:37

标签: angular typescript express angular-ui-router

我正在试图模仿我的后端来开发前端,而我正在使用库expressjs来做这件事。

我有一个名为data.json的文件,其中包含以下对象:

"singleExecutions":[
    {"executionId":190, "label":"exe_190"},
    {"executionId":191, "label":"exe_191"}, 
    ...]

我想路由/executions/executionId类型的请求,以获取列表singleExecutions中具有我需要的特定ID的相应对象。

阅读expressjs的文件,似乎我应该这样做:

const express = require('express');
const data = require('./data');

// 1. Create an express router.
const router = express.Router();

// 2. Handle the requests.
router.get('/executions/:executionId', (req, res) => {
  res.json(data.singleExecutions);
});

// 3. Start the server.
mockBackend.start(router); 

注意:上面的一些变量(例如mockBackend)会在代码段中跳过,但它们没问题,因为上面未显示的其他请求都已正确路由。

当我浏览网址/executions/190时(例如),我得到以下异常:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'executions/190'
Error: Cannot match any routes. URL Segment: 'executions/190'
    at ApplyRedirects.noMatchError (webpack-internal:///../../../router/esm5/router.js:1848)
    at CatchSubscriber.eval [as selector] (webpack-internal:///../../../router/esm5/router.js:1813)
    at CatchSubscriber.error (webpack-internal:///../../../../rxjs/_esm5/operators/catchError.js:108)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at LastSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at ApplyRedirects.noMatchError (webpack-internal:///../../../router/esm5/router.js:1848)
    at CatchSubscriber.eval [as selector] (webpack-internal:///../../../router/esm5/router.js:1813)
    at CatchSubscriber.error (webpack-internal:///../../../../rxjs/_esm5/operators/catchError.js:108)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at MapSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at MapSubscriber.Subscriber.error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:110)
    at LastSubscriber.Subscriber._error (webpack-internal:///../../../../rxjs/_esm5/Subscriber.js:136)
    at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:809)
    at resolvePromise (webpack-internal:///../../../../zone.js/dist/zone.js:775)
    at eval (webpack-internal:///../../../../zone.js/dist/zone.js:858)
    at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:421)
    at Object.onInvokeTask (webpack-internal:///../../../core/esm5/core.js:4956)
    at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:420)
    at Zone.runTask (webpack-internal:///../../../../zone.js/dist/zone.js:188)
    at drainMicroTaskQueue (webpack-internal:///../../../../zone.js/dist/zone.js:595)

我的router.get()肯定会有错误配置的内容,但我无法理解......有人可以帮忙吗?

注意:在相同的代码中,如果我导航GET请求/executions,它会被正确路由:

router.get('/executions', (req, res) => {
  res.json(data.executions);
}); 

......其余的都是一样的。

1 个答案:

答案 0 :(得分:3)

您必须区分角度路线和后端路线。对我而言,您似乎正在导航到与角应用程序对应的主机/端口上的/executions/xxx。角度app上可能不存在此executions路由,因此错误

您需要导航到nodejs用于后端的主机/端口上的那条路由。

假设你在dev中并使用angular-cli,你的角应用程序将在localhost:4200上,你的后端在localhost:4000上;因此您需要导航到http://localhost:4000/executions/xxx

如果您还使用express来提供角度应用程序,使用相同的主机/相同端口,那么您需要确保在注册角度路由之前在快递中注册/executions路由。