嗨,我在本地计算机上有一个基于 Fastify 的本地 dockerized NestJS 应用程序。我想配置外部第三方服务以将数据推送到我的应用程序中的某些端点上。我的应用程序正在使用 HTTP 侦听端口 3000,但第三方服务使用客户端身份验证。他们为我提供了一个 PFX 文件和密码,我想用它来配置一个 HTTPS 端口,该端口将侦听 3333。但是,为了在我的本地计算机上测试它,我在 Thunderclient 和邮递员,具体来说:
我正在向
https://localhost:3333/endpoint/
提出请求。
这是
main.ts
文件 之前 我开始实施 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();
我尝试了几种选择,在线关注这些问题和食谱:
当前版本的
main.ts
文件如下所示:
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();
晚了一年,但也许会对某人有所帮助。 Fastify 库默认仅绑定到本地主机接口。因此,如果您有 dockerized 环境并且应用程序从外部不可见,请尝试指定要绑定的 IP 地址,即
等待app.listen(端口,'0.0.0.0')
监听所有接口...