-
Notifications
You must be signed in to change notification settings - Fork 394
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
feature: Allow to use app.use(createExpressRouter())
instead of useExpressServer(app)
#1352
Comments
app.use(createExpressRouter({ controllers: [...] }))
instead of useExpressServer(app, { controllers: [...] })
app.use(createExpressRouter())
instead of useExpressServer(app)
I use a similar approach at work but I’m just binding to |
Sure:
import { Get, JsonController, createExpressServer } from 'routing-controllers';
import { Express } from 'express';
@JsonController()
class Controller
{
@Get('/api/users')
getUsers() {
console.log('Api controller called');
return [
{ pseudo: 'Hello' },
];
}
}
const app: Express = createExpressServer({
controllers: [
Controller
],
});
app.get('/**', (_, res) => {
console.log('Default controller called');
res.status(200).send('Hello default').end();
});
app.listen(3030, () => {
console.log('Listening on port 3030...');
});
{
"compilerOptions": {
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
} Run: # dependencies
yarn add @types/express@^4.17.21 @types/node@^20.11.20 class-transformer@^0.5.1 class-validator@^0.14.1 express@^4.18.2 routing-controllers@^0.10.4 ts-node@^10.9.2 typescript@^5.3.3
# start app
yarn ts-node app.ts
# make api call
curl http://localhost:3030/api/users Curl result is expected: But I see default controller has been called also, see server logs:
|
The main issue is that we have to call next after the controller otherwise the But unfortunately here we are with that legacy stuff. Your workaround is the best bet for now, even if we provide a I'm going to keep this open because this is something we have to address. |
Another solution could be support for a default controller which would be triggered when no routes could be matched. That way you could define the default page renderer. It would complicate things with |
In my use case, I don't only have a default
I don't know how routing controllers work internally, nor used interceptors yet. I thought that an express router was independant from others routers, so that's why I believe that a
|
Tho they are independent from other middlewares, calling next propagates. Internally we are not using routers but it would not make much difference. |
Description
I'm integrating routing controllers in an existing application to have a better controller structure.
I have lot of controllers, so I don't want to migrate all of them for now.
You fixed an issue where two controllers in routing-controllers were called here: #220
But it is still calling express controller after a annotated controller from routing-controllers:
Both are called. It was not the case before, and it works when I put the second controller in a routing-controller class.
I found this workaround:
Proposed solution
When I started integrating this library, I found we can do either
app = createExpressServer
anduseExpressServer(app)
.But I expected we can do something like
app.use(createExpressRouter({ controllers: [...] }))
.So that we can gradually migrate routers one by one:
And this way we have an independant router than can be used among other express routers.
The text was updated successfully, but these errors were encountered: