Nestjs同时设置http和https服务器 [英] Nestjs sets both http and https servers

查看:43
本文介绍了Nestjs同时设置http和https服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置http和https服务器

已咨询official document

λ nest i

[System Information]
OS Version     : Windows 10
NodeJS Version : v10.16.0
NPM Version    : 6.9.0
[Nest Information]
platform-express version : 6.3.2
common version           : 6.0.0
core version             : 6.0.0

这是我的main.ts文件

import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import * as express from 'express';

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';

async function bootstrap() {
  let httpsOptions = {
    key: fs.readFileSync('D:\localhost.ssh\ajanuw.local.key'),
    cert: fs.readFileSync('D:\localhost.ssh\ajanuw.local.crt'),
  };

  const server = express();
  const app = await NestFactory.create(
    AppModule, 
    new ExpressAdapter(server), // error
  );
  app.enableCors();
  await app.init();

  http.createServer(server).listen(3000);
  https.createServer(httpsOptions, server).listen(443);
}
bootstrap();

但编辑器出现错误消息

无法将"ExpressAdapter"类型的参数分配给"AbstractHttpAdapter"类型的参数。 *属性"instance"受保护,但类型"AbstractHttpAdapter"不是从"AbstractHttpAdapter"派生的类。

如何处理此错误,谢谢

推荐答案

我认为您混合了两种不同的方法来创建服务器。 在您的示例中,代码应如下所示:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as fs from 'fs';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as http from 'http';
import * as https from 'https';
import * as express from 'express';

async function bootstrap() {
  const privateKey = fs.readFileSync('D:\localhost.ssh\ajanuw.local.key', 'utf8');
  const certificate = fs.readFileSync('D:\localhost.ssh\ajanuw.local.crt', 'utf8');
  const httpsOptions = {key: privateKey, cert: certificate};

  const server = express();
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(server),
  );
  await app.init();

  http.createServer(server).listen(3000);
  https.createServer(httpsOptions, server).listen(443);
}

bootstrap();

这篇关于Nestjs同时设置http和https服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆