Combine a peer server with nestjs application #1226
-
I'm having an issue:Hello, I am trying to combine peer server with my nestjs application. Unfortunately it doesn't work as expected. I am creating a service containing the peer server instance and initialize it on application start. I also use this service to handle requests coming main.ts import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { I18nMiddleware } from 'nestjs-i18n';
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';
import { AppModule } from './app.module';
import { PeerServerService } from './peer-server/peer-server.service';
import { PrismaService } from './prisma/prisma.service';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
bufferLogs: true,
});
app.useLogger(app.get(Logger));
app.useGlobalInterceptors(new LoggerErrorInterceptor());
app.use(I18nMiddleware);
const prismaService = app.get(PrismaService);
const peerServerService = app.get(PeerServerService);
prismaService.enableShutdownHooks(app);
peerServerService.enablePeerServer(app);
await app.listen(3000);
}
bootstrap(); peer-server.service.ts import { Injectable } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ExpressPeerServer, PeerServerEvents } from 'peer';
import { Express } from 'express';
@Injectable()
export class PeerServerService {
peerServer: Express & PeerServerEvents;
enablePeerServer(app: NestExpressApplication) {
this.peerServer = ExpressPeerServer(app.getHttpServer(), {
path: '/myapp',
});
console.log('peer server: ', this.peerServer);
this.peerServer.get('/test', (req, res) => {
res.send('hello');
});
}
} peer-server.controller.ts import { All, Controller, Next, Req, Res } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
import { PeerServerService } from './peer-server.service';
@Controller('/peer-server')
export class PeerServerController {
constructor(private readonly peerServerService: PeerServerService) {}
@All('*')
server(
@Req() request: Request,
@Res() response: Response,
@Next() next: NextFunction,
) {
const entryPointPath = '/peer-server/';
request.url = request.url.replace(entryPointPath, '/');
console.log('in route peer: ', request.url);
this.peerServerService.peerServer(request, response, next);
}
} I verified that the server is correctly forwarded to the peer service with this request this.peerServer.get('/test', (req, res) => {
res.send('hello');
}); Sending a request to Has anyone ever done that successfully ? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Hey @lukadriel7, I added a guide for NestJS to our new documentation. Hope that helps :) |
Beta Was this translation helpful? Give feedback.
-
I foget to run PeerServerService.the doc should add the step what is |
Beta Was this translation helpful? Give feedback.
-
Hello! I'm also trying to run a peerjs server on a nestjs project, but I can't access the guide referenced here. The link appears to be broken... Is there any other place where I can find such guide? if not, would you please share its contents here? thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
Hey there, @lukadriel7 , @throoze , @MicroMatrixOrg , @jonasgloning , @hansoksendahl I am currently trying to integrate Peer Server into a NestJS project, but I'm facing issues with the configuration. When I try to make a request to the url, it returns a 404 error. I am unsure if the problem is due to misconfiguration or a misunderstanding of how to properly set up Peer Server with NestJS. Could you please provide a detailed guide or step-by-step instructions on how to configure this correctly? Here is the code I have so far: channel.module.ts import { Module } from '@nestjs/common';
import { ChannelController } from './channel.controller';
import { ChannelService } from './channel.service';
@Module({
exports: [ChannelService],
controllers: [ChannelController],
providers: [ChannelService],
})
export class ChannelModule {} channel.controller.ts import { All, Controller, Next, Req, Res } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
import { ChannelService } from './channel.service';
@Controller({
path: 'channel',
version: '1',
})
export class ChannelController {
constructor(private readonly channel: ChannelService) {}
@All('*')
server(
@Req() request: Request,
@Res() response: Response,
@Next() next: NextFunction,
) {
const entryPointPath = '/channel/';
request.url = request.url.replace(entryPointPath, '/');
return this.channel.peerServer(request, response, next);
}
} channel.service.ts import { Injectable } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ExpressPeerServer, PeerServerEvents } from 'peer';
import { Express } from 'express';
@Injectable()
export class ChannelService {
peerServer: Express & PeerServerEvents;
enablePeerServer(app: NestExpressApplication) {
this.peerServer = ExpressPeerServer(app.getHttpServer(), {
path: '/channel',
});
}
} app.module.ts import { MiddlewareConsumer, Module, NestModule, Logger } from '@nestjs/common';
import { ChannelModule } from './channel/channel.module';
@Module({
imports: [ChannelModule],
controllers: [AppController],
providers: [Logger],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*');
}
} main.ts import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import { instance as winstonInstance } from './utils/logger.utils';
import { ChannelService } from './channel/channel.service';
import { NestExpressApplication } from '@nestjs/platform-express';
const PORT = process.env.PORT || 3000;
const API_SERVICE = process.env.API_SERVICE;
const BASE_URL = `${API_SERVICE}/api`;
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: WinstonModule.createLogger({
instance: winstonInstance,
}),
});
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.setGlobalPrefix(BASE_URL);
app.enableVersioning({
type: VersioningType.URI,
});
const channel = app.get(ChannelService);
channel.enablePeerServer(app);
await app.listen(PORT);
}
bootstrap(); Here are the URLs I have tried so far -
I would appreciate your help in resolving this issue! |
Beta Was this translation helpful? Give feedback.
Hey @lukadriel7, I added a guide for NestJS to our new documentation. Hope that helps :)