【问题标题】:Serving public and private ports using Nestjs使用 Nestjs 服务公共和私有端口
【发布时间】:2020-02-04 16:36:26
【问题描述】:

我正在构建一个旨在为移动应用程序提供服务的应用程序。除了为客户提供服务外,它还将具有多种后台功能。

我们正在使用swagger,并且我们确实希望能够访问我们后台端点的swagger 文档。但是,我们不想公开所有端点。

假设让所有端点都公开是一个糟糕的选择,我们正在考虑的一种解决方案是让我们的服务器服务两个端口,然后只向公众公开一个端口。我们创建了一个小型 sample repo,它在两个不同的端口上为客户端模块和后台模块提供服务。

main.ts 如下所示:

import { NestFactory } from '@nestjs/core';
import { ClientModule } from './modules/client/client.module';
import * as express from 'express';
import * as http from 'http';
import {ExpressAdapter} from '@nestjs/platform-express';
import { BackOfficeModule } from './modules/backoffice/backoffice.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {

  const clientServer = express();
  const clientApp = await NestFactory.create(
    ClientModule,
    new ExpressAdapter(clientServer),
  );
  const clientOptions = new DocumentBuilder()
    .setTitle('ClientServer')
    .setDescription('The client server API description')
    .setVersion('1.0')
    .addTag('client')
    .build();
  const clientDocument = SwaggerModule.createDocument(clientApp, clientOptions);
  SwaggerModule.setup('api', clientApp, clientDocument);
  await clientApp.init();

  const backOfficeServer = express();
  const backOfficeApp = await NestFactory.create(
    BackOfficeModule,
    new ExpressAdapter(backOfficeServer),
  );

  const backOfficeOptions = new DocumentBuilder()
    .setTitle('BackOffice')
    .setDescription('The back office API description')
    .setVersion('1.0')
    .addTag('backOffice')
    .build();
  const backOfficeDocument = SwaggerModule.createDocument(backOfficeApp, backOfficeOptions);
  SwaggerModule.setup('api', backOfficeApp, backOfficeDocument);
  await backOfficeApp.init();

  http.createServer(clientServer).listen(3000); // The public port (Load balancer will route traffic to this port)
  http.createServer(backOfficeServer).listen(4000); // The private port (Will be accessed through a bastian host or similar)
}
bootstrap();

另一种选择是创建代码库和基础架构的更大分离,但由于这是一个非常早期的阶段,我们认为这是不必要的。

因此,我们向Nest 社区提出的问题是,有人这样做了吗?如果有,你的经验是什么?像这样分离我们的后端代码有什么缺点?

【问题讨论】:

    标签: express nestjs


    【解决方案1】:

    NestJS 文档介绍了如何让一台服务器服务于多个端口: https://docs.nestjs.com/faq/multiple-servers#multiple-simultaneous-servers

    以下秘籍展示了如何实例化一个同时侦听多个端口(例如,一个非 HTTPS 端口和一个 HTTPS 端口)的 Nest 应用程序。

    const httpsOptions = {
        key: fs.readFileSync('./secrets/private-key.pem'),
        cert: fs.readFileSync('./secrets/public-certificate.pem'),
    };
    
    const server = express();
    const app = await NestFactory.create(
        ApplicationModule,
        new ExpressAdapter(server),
    );
    await app.init();
    
    http.createServer(server).listen(3000);
    https.createServer(httpsOptions, server).listen(443);
    

    【讨论】:

      【解决方案2】:

      没关系,但如果你想在一台主机上运行两台服务器,我建议创建两个文件,如 main-client.tsmain-back-office.ts 并在不同的进程中运行它们,因为在这种情况下,一台服务器的故障不会影响他人的工作。

      另外,如果您没有在 Docker 中运行它,我建议您使用像 foreverpm2supervisor 或我自己的非常小的库 workers-cluster 这样的工具

      如果您在 Docker 中运行它并且不想进行大重构,我建议您创建 单个 Dockerfile 运行不同的 CMDENTRYPOINT 命令

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 2019-12-18
        • 2020-01-03
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        相关资源
        最近更新 更多