Skip to content

feature: Allow to use app.use(createExpressRouter()) instead of useExpressServer(app) #1352

Open
@alcalyn

Description

@alcalyn

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:

    @Get('/api/users')
    getAll()
    {
        return userReporitory.findAll();
    }

    app.get('/**', async (_, res) => {
        res.render('page.ejs'); // render main app html page for html5 navigation
    });

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:

    useExpressServer(app, {...});

    // If an api endpoint has already been called, stop routing chain
    app.all('/**', (req, res, next) => {
        if (!res.headersSent) {
            next();
        }
    });

    // other express routers

    app.get('/**', async (_, res) => {
        res.render('page.ejs'); // render main app html page for html5 navigation
    });

Proposed solution

When I started integrating this library, I found we can do either app = createExpressServer and useExpressServer(app).

But I expected we can do something like app.use(createExpressRouter({ controllers: [...] })).

So that we can gradually migrate routers one by one:

    app.use(staticsRouter());

     // Inside myApiRouter(), I can use either routing-controller, like return createExpressRouter(...)
    // or normal express like const router = Router(); ... ; return router;
    app.use(myApiRouter());

    app.use(pwaRouter());
    app.use(seoRouter());
    app.use(pagesRouter());

And this way we have an independant router than can be used among other express routers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions