The problem
Hi, I have a local dockerized NestJS app over Fastify in my local machine. I want to configure an external third party service to push data on some endpoints in my app. My app is listening on port 3000 using HTTP, but the third party service uses client authentication. They have provided me with a PFX file and passphrase which I want to use to configure an HTTPS port that will listen on, let's say, 3333. However,to test it out on my local machine, I am getting an error on both Thunderclient and Postman, specifically:
- Postman: Error: Client network socket disconnected before secure TLS connection was established
- Thunderclient: Connection was forcibly closed by a peer.
I am making requests to https://localhost:3333/endpoint/
.
Here's the main.ts
file BEFORE I started implementing HTTPS.
import { NestFactory } from '@nestjs/core';import { NestFastifyApplication } from '@nestjs/platform-fastify';import { AppModule } from './app.module';import { utilities as nestWinstonModuleUtilities, WinstonModule,} from 'nest-winston';import * as winston from 'winston';import * as winstonDailyRotateFile from 'winston-daily-rotate-file';async function bootstrap() { const app = await NestFactory.create<NestFastifyApplication>(AppModule, { logger: globalLogger }); await app.startAllMicroservices(); await app.listen(3000); process.on('uncaughtException', function (err) { globalLogger.error(JSON.stringify(err, null, 2)) globalLogger.log("UNCAUGHT EXCEPTION: Node NOT Exiting..."); });}const globalLogger = WinstonModule.createLogger({ // ... Logger configuration})bootstrap();
What I tried
I have tried several options, following these issues and recipes online:
The current version of the main.ts
file looks like this:
import { NestFactory } from '@nestjs/core';import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';import { AppModule } from './app.module';import { httpsModule } from './httpsmodule/httpsmodule.module';import { utilities as nestWinstonModuleUtilities, WinstonModule,} from 'nest-winston';import * as winston from 'winston';import * as winstonDailyRotateFile from 'winston-daily-rotate-file';import { readFileSync, accessSync, constants } from 'fs';async function bootstrap() { try { const userAuthCertificateFilePath = "/src/ssl/UserCloudAuth.p12"; accessSync(userAuthCertificateFilePath, constants.F_OK | constants.R_OK) const certificatePassword = "somePassword" const userAuthCertificateFilePath = process.env.CERTIFICATE_FILE_PATH; const certificatePassword = process.env.CERTIFICATE_PASSWORD; const httpsOptions = { pfx: readFileSync(userAuthCertificateFilePath), passphrase: certificatePassword } const httpApp = await NestFactory.create<NestFastifyApplication>( AppModule, { logger: globalLogger } ); await httpApp.startAllMicroservices(); const httpsApp = await NestFactory.create<NestFastifyApplication>( httpsModule, new FastifyAdapter({ https: httpsOptions }) ); await httpsApp.startAllMicroservices(); await httpApp.listen(3000); await httpsApp.listen(3333); process.on('uncaughtException', function (err) { globalLogger.error(JSON.stringify(err, null, 2)) globalLogger.log("UNCAUGHT EXCEPTION: Node NOT Exiting..."); }); } catch (error) { globalLogger.error(`Main: ${error}`) }}const globalLogger = WinstonModule.createLogger({ // ... // ... Logger configuration})bootstrap();