nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript πŸš€
https://nestjs.com
MIT License
67.6k stars 7.61k forks source link

Using app without start web application #201

Closed emilianosantucci closed 6 years ago

emilianosantucci commented 7 years ago

Hi all, I want use this gorgeous framework inside my application but I've some questions to ask:

  1. Is it possible to use Nest w/out start web application? If the answer is yes, how?
  2. How can get components instance outside Nest context for using in parent application with benefits of DI?
wbhob commented 7 years ago

I'd be happy to answer your questions.

  1. You can migrate your existing express application to Nest by corresponding the method and path, as well as the function arguments, to Nest-style syntax. So if you have app.get('/hello', function(req,res) {}), you can use this style:
@Get()
@Path('/hello')
sayHello(@Request() req, @Response() res) {
    // whatever you want
}
  1. You won't be able to use DI outside the context of Nest. Depending on the size of your app, it may take just a few hours to convert all of your routes, but at this time, they must all be migrated at once.

I hope this helps!

emilianosantucci commented 7 years ago

Thank you @wbhob for your help. Maybe talk about my app can explain more what I need.

I'm starting develop an application with Moleculer and my code now is only for a PoC purpose. This framework has ability to service auto-discovery, REPL integration and more features for the microservice "world" like a charm. But (there is ever a "but"), talking about how build a microservice, don't have the flexibility as Nest to write clean and re-usable code.

My purpose is to combine this frameworks to take advantage from both: Nest to build a great microservices and Moleculer for manage them.

During my study about do that, I've found only one formula, but I hope there are other:

  1. Develop microservice (or app) with Nest and launch in listening mode.
  2. Develop microservice skeleton with Moleculer and, inside actions, use a ClientProxy for communicate with Nest app.

Personally, I don't like this idea of architecture, too much "jumps" to reach the goal, with many communication errors prone. So my goal is to handle the Nest application, if possible, inside Moleculer microservice without client/server communication (like a service, for intends).

Would you think about? Any suggestions?

wbhob commented 7 years ago

Nest microservices aren't actually the modern definition of "microservices", as indicated in the docs. However, I see an opportunity for a module here! I'm talking to the project creator about joining the core team and I think that would be a good place to start!

kamilmysliwiec commented 7 years ago

Hi @emilianosantucci, As far as I understand you wanna access the modules/components from outside the Nest application zone/context - just do not use any transport layer but take advantages from the modularity / DI things. Am I right?

emilianosantucci commented 7 years ago

Exactly @kamilmysliwiec. in Moleculer is possible to attach an external third-part microservice but you need to respect some convention (like a health states, actions, ...). I'm also an Angular.io and Java developer and I think that Nest have a great IoC, DI (like Spring for Java) and module system not just for using on web application. Is a great opportunity to take this advantage out-of-box because there is no other frameworks do that in NodeJS (TypeDI cover only the DI with TypeScript).

kamilmysliwiec commented 7 years ago

Hi @emilianosantucci, I'm strongly thinking about a right solution. What is your opinion about this proposal - add a method which responsibility is to create an application but instead of attaching http server / microservice, it'd only return the RootModule instance so you can get() any component which belongs to its?

emilianosantucci commented 7 years ago

It seems to be the right approach, especially if anyone can get(...) a component / module without take care about right DI onto outside context. In this case anyone will be able to create an eco-system of modules and use them where we need, also as npm dependency.

I've only a consideration to do (just for implement it with enterprise vision): if I request the RootModule into two separate slice of code inside the same application, what obtain? Two separate contexts or just a shared context?

I think that "shared context" is the right one but I consider the (possible) trouble to do that. One solution is to move that issue on outside context, taking care to create a shared class as a component factory.

emilianosantucci commented 6 years ago

Hi @kamilmysliwiec, any news about that?

Can I help for something?

kamilmysliwiec commented 6 years ago

In progress 🐀

kamilmysliwiec commented 6 years ago

This feature is waiting for the next release. Hope you'll enjoy πŸ™‚

emilianosantucci commented 6 years ago

I embrance it where is avaiable and give you feedback in case. Thanks for your work. Can I close this issue or prefer to take it open until feature released?

kamilmysliwiec commented 6 years ago

Let's keep it opened for now πŸ™‚

kamilmysliwiec commented 6 years ago

4.4.0 is published now!

Zeldaze commented 6 years ago

Hi. I’m interested in this too. Any example of its usage now that it’s been published? @kamilmysliwiec

Zeldaze commented 6 years ago

@emilianosantucci Did you manage to get this working? Would you mind provided an example?

emilianosantucci commented 6 years ago

@Zeldaze not yet, but you can find its usage on project changelog. I think @kamilmysliwiec provide an example on official documentation in the next weeks, if I can't make it before.

lock[bot] commented 5 years ago

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.