Two routes beign called at the same time
juliohintze opened this issue · comments
juliohintze commented
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 commented
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