Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two routes beign called at the same time #164

Open
juliohintze opened this issue Sep 23, 2021 · 1 comment
Open

Two routes beign called at the same time #164

juliohintze opened this issue Sep 23, 2021 · 1 comment

Comments

@juliohintze
Copy link

juliohintze commented Sep 23, 2021

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?

@JosePedroValentimOliveira

This is because it sees the route /foo/bar as /foo/:word. Because that foo could als be a word you are inputting. You need to specify the route with the pathparam better to not have that confusion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants