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

Adding routes dynamically #124

Closed
infiniteshroom opened this issue Jul 29, 2017 · 5 comments
Closed

Adding routes dynamically #124

infiniteshroom opened this issue Jul 29, 2017 · 5 comments

Comments

@infiniteshroom
Copy link

Hi guys. Just discovered this framework today and it's awesome, really like that it allows me to follow the same structure and setup as angular on the back end. Can't wait to get into the meat of doing something with it.

Got a question though regarding routing. From what I can see from the docs, the only way to add routes is by using a decorator on the controller. I was wondering if at all its possible to define routes dynamically.

For instance in laravel we can do something like this:

Route::get($uri, $callback);

Cheers,
Mark.

@cristianmartinez
Copy link

Hi @infiniteshroom,

Yes, it is possible. Nest uses behind scenes ExpressJS as HTTP module. This allows you call the factory method with an explicit express instance and use it as you want. I don't think this an idiomatic way to do it with Nest, but it will work.

....
const expressInstance = express();
expressInstance.use(morgan('dev'));
const app = NestFactory.create(ApplicationModule, expressInstance);
expressInstance.get('/foo', function (req, res) {
    res.send('bar');
})
...

@infiniteshroom
Copy link
Author

Thanks @cristianmartinez. This looks to be what I'm after, much appreciated. Do you know if this will also allow me to call nest controller methods?

@cristianmartinez
Copy link

@infiniteshroom I think you can do it, but you will lose all the power of the Nest Controller. Nest uses internally a container which resolves all dependencies meta-data assigned by the decorators. If you want to call the controller directly there is no interpretation of that. I was trying to get the container but this is a private instance of the application.

On Laravel, you can have the container instance on each Provider (Think as a Provider as a standard way to connect your custom code with the framework), it allows to dynamically register and gets instances of the container and for me, this approach is very flexible and powerful.

I hope one day something like that comes to this awesome framework @kamilmysliwiec :)

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Sep 12, 2017

Hi @infiniteshroom,
Unfortunately, I don't plan to introduce a possibility to add routes dynamically.
Thanks + thanks @cristianmartinez, Kamil

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants