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