Skip to content

Two routes beign called at the same time #164

@juliohintze

Description

@juliohintze

Consider the following code:

import express from 'express';
import { Server, Path, GET, PathParam } from 'typescript-rest';

@Path('/foo')
class FooService {
  @Path('bar')
  @GET
  bar() {
    console.log('/foo/bar called');
    return 'Bar';
  }

  @Path(':word')
  @GET
  customWord(@PathParam('word') word: string): string {
    console.log(`/foo/${word} called`);
    return word;
  }
}

const app: express.Application = express();
Server.buildServices(app);

Here we have two routes: /foo/bar and /foo/:word.
The problem I'm facing is that when I call /foo/bar, both routes are called.
If I'm using vanilla express, this problem does NOT happen. The code:

import express, { Router } from 'express';

const app: express.Application = express();
const router = Router();

router.get('/bar', (req, res) => {
  console.log('/foo/bar called');

  res.send('bar');
});

router.get('/:word', (req, res) => {
  console.log(`/foo/${req.params.word} called`);

  res.send(req.params.word);
});

app.use('/foo', router);

app.listen(3000, function () {
  console.log('Rest Server listening on port 3000!');
});

I'm using typescript-rest version 3.0.4.

Seems like a bug, but am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions