0

I am trying to run two simultaneous servers on a NestJS app running with the @nestjs/platform-fastify integration (specifically I am trying to run one HTTP and one HTTPS server of the same app).

NestJS documentation includes multiple-servers for express-integration but does not mention how to do this with fastify (https://docs.nestjs.com/faq/multiple-servers). Does anyone know the correct implementation to be able to do this?

jengel
  • 323
  • 4
  • 10
  • There is no way as i know. You can use http->https redirect plugin or run two separate servers. – ZiiMakc Nov 20 '19 at 11:51

1 Answers1

2

Edit 11/20/19

So it appears my original answer was incorrect. I for the most part was just going off the docs and trying to piece something together, forgetting that Fastify's server instance is different than that of Express's and because of that is incompatible with the built in http(s) module(s). If you don't mind having to let NestJS set up your dependencies again, you can always do something like this:

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { readFileSync } from 'fs';
import { join } from 'path';
import fastify = require('fastify');

const httpsOptions = {
  key: readFileSync(join(__dirname, '..', 'secrets', 'ssl.key')),
  cert: readFileSync(join(__dirname, '..', 'secrets', 'ssl.crt')),
};

async function bootstrap() {
  const fastifyHttps = fastify({https: httpsOptions});
  const httpApp = await NestFactory.create(AppModule , new FastifyAdapter() );
  const httpsApp = await NestFactory.create(AppModule, new FastifyAdapter(fastifyHttps));
  await httpApp.listen(3000);
  console.log('http running on port 3000');
  await httpsApp.listen(3333);
  console.log('https running on port 3333');
}
bootstrap();

And this time, I tested it to make sure it works.


This only works with Express

Nest provides methods to get the server instance back after creating the application with app.getHttpServer() so that you can get your server veriable no matter what. Due to this you can set up your application as you normally would with your fastify adapter and the after running await app.init() you can get the server instance. From there you can create the server and set the listening port with the http and https built in packages like the docs are showing with express.

const httpsOptions = {
  key: fs.readFileSync('./secrets/private-key.pem'),
  cert: fs.readFileSync('./secrets/public-certificate.pem'),
};

const app = await NestFactory.create(
  ApplicationModule
);
await app.init();

const server = app.getHttpServer();

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

And that should do the trick

Community
  • 1
  • 1
Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • This doesn't seem to work for me. What version of @nest/core are you using? I am on v6.10.1 and @nestjs/platform-fastify v6.10.1 – jengel Nov 20 '19 at 21:22
  • Answer has been updated. I forgot Fastify and Express have different underlying server architectures – Jay McDoniel Nov 21 '19 at 00:47