Provide a NestJS boilerplate with hybrid application support for both HTTP and microservices. It should listen on port 3000 for REST and port 5000 as a gRPC server, sharing the same service layer between both transports.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a NestJS boilerplate with hybrid support for HTTP (port 3000) and gRPC microservice (port 5000), sharing the same AppService between HTTP and gRPC controllers.
ts
// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/microservices';
import { join } from 'path';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
package: 'hero',
protoPath: join(__dirname, '../proto/hero.proto'),
url: '0.0.0.0:5000',
},
});
await app.startAllMicroservices();
await app.listen(3000);
console.log('HTTP server listening on http://localhost:3000');
console.log('gRPC server listening on 0.0.0.0:5000');
}
bootstrap();
ts
// src/app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { HeroGrpcController } from './hero-grpc.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController, HeroGrpcController],
providers: [AppService],
})
export class AppModule {}
// proto/hero.proto
syntax = "proto3";
package hero;
service HeroService {
rpc findOne (HeroById) returns (Hero) {}
}
message HeroById {
int32 id = 1;
}
message Hero {
int32 id = 1;
string name = 2;
}
This setup makes the same AppService provide hero data to both RESTful HTTP routes on port 3000 and a gRPC server on port 5000, using Nest's hybrid application support.
Below is a minimal NestJS hybrid setup that serves both HTTP (REST) on port 3000 and gRPC on port 5000, sharing the same service layer.
syntax = "proto3";
package hero;
service HeroService {
rpc FindOne (HeroById) returns (Hero) {}
}
message HeroById {
int32 id = 1;
}
message Hero {
int32 id = 1;
string name = 2;
}
src/app.service.ts
typescript
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
// In a real app, fetch from DB or other data source
private heroes = [
{ id: 1, name: 'Batman' },
{ id: 2, name: 'Wonder Woman' },
{ id: 3, name: 'Superman' },
];
findOne(id: number) {
return this.heroes.find(hero => hero.id === id) || null;
}
}
src/app.controller.ts (REST controller)
typescript
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import { AppService } from './app.service';
@Controller('hero')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get(':id')
getHero(@Param('id', ParseIntPipe) id: number) {
// Shared service for both REST and gRPC
return this.appService.findOne(id);
}
}
src/hero-grpc.controller.ts (gRPC controller)
typescript
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import { AppService } from './app.service';
@Controller()
export class HeroGrpcController {
constructor(private readonly appService: AppService) {}
// Method name must match the one in the proto file
@GrpcMethod('HeroService', 'FindOne')
findOne(data: { id: number }) {
return this.appService.findOne(data.id);
}
}
src/app.module.ts
typescript
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { HeroGrpcController } from './hero-grpc.controller';
@Module({
imports: [],
controllers: [AppController, HeroGrpcController],
providers: [AppService],
})
export class AppModule {}
src/main.ts
typescript
import { join } from 'path';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
async function bootstrap() {
// Create the main HTTP (REST) app
const app = await NestFactory.create(AppModule);
// Attach a gRPC microservice on port 5000
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
url: '0.0.0.0:5000',
package: 'hero',
protoPath: join(__dirname, './proto/hero.proto'),
},
});
// Start all microservices (gRPC) and then start HTTP server
await app.startAllMicroservices();
await app.listen(3000, () => {
console.log('HTTP server is running on http://localhost:3000');
console.log('gRPC server is running on 0.0.0.0:5000');
});
}
bootstrap();
How it works:
NestFactory.create(AppModule) spins up the REST server on port 3000.
app.connectMicroservice(...) configures a gRPC transport on port 5000.
Both controllers (AppController for REST and HeroGrpcController for gRPC) depend on the same AppService, ensuring a shared service layer.
app.startAllMicroservices() starts the gRPC server before the HTTP server begins listening.
You can test REST with:
code
GET http://localhost:3000/hero/1
And gRPC by calling FindOne on HeroService at 0.0.0.0:5000 using the hero.proto definition.