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: 0 comments 4 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.
Hey @lukadriel7, I added a guide for NestJS to our new documentation. Hope that helps :)