首页 > 解决方案 > 使用同一个项目 Adonisjs 同时启动 http 和 https

问题描述

我希望在我的 adonisjs 项目中同时运行 HTTP 和 HTTPS。现在我的 server.ts 看起来像这样(运行 https):

import 'reflect-metadata'
import sourceMapSupport from 'source-map-support'
import { Ignitor } from '@adonisjs/core/build/standalone'
import { createServer } from "https";
import { readFileSync } from 'fs';
import { join } from 'path';

sourceMapSupport.install({ handleUncaughtExceptions: false })

const privateKey = readFileSync(join(__dirname + '/sslCert/server.pem'), 'utf8');
const certificate = readFileSync(join(__dirname + '/sslCert/server.cert'), 'utf8');
new Ignitor(__dirname).httpServer().start((handle) => {
    return createServer(
      {
        key: privateKey,
        cert: certificate,
      },
      handle
    );
  });

例如,我希望 https 服务器在 3333 端口上运行,http 服务器在 4444 端口上运行。

标签: node.jstypescriptadonis.js

解决方案


我发现可以使用相同的应用程序创建一个 http 实例,但在我的情况下,我只想要特定路由的 http 和其他路由的 https。因此,拥有 http 路由的最优化方式是完全创建一个实例,这样我们就不必在不会在 http 请求中使用的 http 路由旁边加载 https 路由。


推荐阅读