Nestjs 设置https

只是用https

import * as fs from 'fs';

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

const httpsOptions = {
  key: fs.readFileSync('D:/localhost_ssl/dev.ajanuw.com.key'),
  cert: fs.readFileSync('D:/localhost_ssl/dev.ajanuw.com.crt'),
};

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    httpsOptions,
  });
  app.enableCors();
  // 我配置了hosts文件,让dev.ajanuw.com指向127.0.0.1
  console.log(`https://dev.ajanuw.com:3000/`);
  await app.listen(3000);
}
bootstrap();

http和https

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


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

const httpsOptions = {
  key: fs.readFileSync('D:/localhost_ssl/dev.ajanuw.com.key'),
  cert: fs.readFileSync('D:/localhost_ssl/dev.ajanuw.com.crt'),
};

async function bootstrap() {
  const server = express();

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

  app.setGlobalPrefix('api');
  app.enableCors();

  await app.init();

  console.log(`http://dev.ajanuw.com:3000`);
  console.log(`https://dev.ajanuw.com`);
  http.createServer(server).listen(3000);
  https.createServer(httpsOptions, server).listen(443);
}
bootstrap();

如果要访问http:http://dev.ajanuw.com:3000,https:https://dev.ajanuw.com

posted @ 2020-04-09 10:24  Ajanuw  阅读(2196)  评论(0编辑  收藏  举报