NestFactory is the entry point for every nestrs application. You call one of its static methods with a root module type parameter, and it builds the ProviderRegistry, wires controllers into an Axum Router, and returns either a NestApplication builder or a MicroserviceApplication handle that you configure and then start listening.
use nestrs::prelude::*;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create::<AppModule>()
.listen(3000)
.await;
}
NestFactory::create
Creates an HTTP application from a static root module type.
pub fn create<M: Module>() -> NestApplication
The root module type. Must implement the Module trait, which is generated by #[module(...)].
M::build() runs at call time: it walks the module import graph, registers all providers into a ProviderRegistry, and registers all controllers into an Axum Router. The returned NestApplication is a builder; no network socket is opened until you call listen, listen_graceful, or listen_with_shutdown.
use nestrs::prelude::*;
#[derive(Default)]
#[injectable]
struct AppState;
#[controller(prefix = "/health")]
struct HealthController;
#[routes(state = AppState)]
impl HealthController {
#[get("/")]
async fn ping() -> &'static str { "ok" }
}
#[module(controllers = [HealthController], providers = [AppState])]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create::<AppModule>()
.listen(3000)
.await;
}
NestFactory::create_with_modules
Creates an HTTP application from a static root module plus a collection of runtime-selected DynamicModule instances.
pub fn create_with_modules<M, I>(dynamic_modules: I) -> NestApplication
where
M: Module,
I: IntoIterator<Item = DynamicModule>,
The static root module type.
dynamic_modules
IntoIterator<Item = DynamicModule>
Zero or more dynamic modules to merge into the root module’s registry and router.
Use this when you need to conditionally include feature routers or plug-in modules that are only known at runtime—for example, when a feature flag is read from the environment after startup.
use nestrs::prelude::*;
#[module]
struct AppModule;
#[module(controllers = [], providers = [])]
struct AdminModule;
#[tokio::main]
async fn main() {
let dynamic = if std::env::var("ENABLE_ADMIN").is_ok() {
vec![DynamicModule::from_module::<AdminModule>()]
} else {
vec![]
};
NestFactory::create_with_modules::<AppModule, _>(dynamic)
.listen(3000)
.await;
}
NestFactory::create_microservice (TCP)
Creates a microservice application using the TCP transport adapter. Requires the microservices feature.
pub fn create_microservice<M>(options: TcpMicroserviceOptions) -> MicroserviceApplication
where
M: Module + MicroserviceModule,
M
impl Module + MicroserviceModule
The root module type. Must also implement MicroserviceModule, generated when you declare microservices = [...] in #[module].
TCP server configuration (host, port, and related settings).
use nestrs::prelude::*;
use nestrs::microservices::TcpMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice::<AppModule>(
TcpMicroserviceOptions::default(),
)
.listen()
.await;
}
Call .also_listen_http(port) on the returned MicroserviceApplication to run both an HTTP router and the microservice transport in a single process (hybrid mode).
NestFactory::create_microservice_nats
Creates a microservice application using the NATS transport adapter. Requires the microservices-nats feature.
pub fn create_microservice_nats<M>(options: NatsMicroserviceOptions) -> MicroserviceApplication
where
M: Module + MicroserviceModule,
NATS connection and subject configuration.
# Cargo.toml
[dependencies]
nestrs = { version = "*", features = ["microservices", "microservices-nats"] }
use nestrs::prelude::*;
use nestrs::microservices::NatsMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice_nats::<AppModule>(
NatsMicroserviceOptions::default(),
)
.listen()
.await;
}
NestFactory::create_microservice_redis
Creates a microservice application using the Redis Pub/Sub transport adapter. Requires the microservices-redis feature.
pub fn create_microservice_redis<M>(options: RedisMicroserviceOptions) -> MicroserviceApplication
where
M: Module + MicroserviceModule,
Redis connection and channel configuration.
use nestrs::prelude::*;
use nestrs::microservices::RedisMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice_redis::<AppModule>(
RedisMicroserviceOptions::default(),
)
.listen()
.await;
}
NestFactory::create_microservice_grpc
Creates a microservice application using the gRPC transport adapter. Requires the microservices-grpc feature.
The gRPC transport carries JSON-encoded payloads (pattern + payload_json) inside protobuf, matching the nestrs-microservices::wire module’s request/response shapes.
pub fn create_microservice_grpc<M>(options: GrpcMicroserviceOptions) -> MicroserviceApplication
where
M: Module + MicroserviceModule,
Bind address and TLS configuration for the tonic gRPC server.
use nestrs::prelude::*;
use nestrs::microservices::GrpcMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice_grpc::<AppModule>(
GrpcMicroserviceOptions::default(),
)
.listen()
.await;
}
NestFactory::create_microservice_rabbitmq
Creates a microservice application using the RabbitMQ transport adapter. Requires the microservices-rabbitmq feature.
pub fn create_microservice_rabbitmq<M>(
options: RabbitMqMicroserviceOptions,
) -> MicroserviceApplication
where
M: Module + MicroserviceModule,
options
RabbitMqMicroserviceOptions
AMQP connection URL, exchange, and queue configuration.
use nestrs::prelude::*;
use nestrs::microservices::RabbitMqMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice_rabbitmq::<AppModule>(
RabbitMqMicroserviceOptions::default(),
)
.listen()
.await;
}
MicroserviceApplication builder methods
All create_microservice_* methods return a MicroserviceApplication. It exposes the following builder methods before you call listen:
| Method | Description |
|---|
.also_listen_http(port: u16) | Run an HTTP router alongside the microservice transport in the same process |
.configure_http(f) | Apply NestApplication builder methods before HTTP starts |
.get::<T>() | Resolve a provider from the DI container (returns Arc<T>) |
use nestrs::prelude::*;
use nestrs::microservices::TcpMicroserviceOptions;
#[module]
struct AppModule;
#[tokio::main]
async fn main() {
NestFactory::create_microservice::<AppModule>(TcpMicroserviceOptions::default())
.also_listen_http(3001)
.configure_http(|app| {
app.set_global_prefix("api")
.enable_health_check("/health")
})
.listen()
.await;
}