HTTPS

In order to create application that uses HTTPS protocol, we have to pass an options object:

  1. const httpsOptions = {
  2. key: fs.readFileSync('./secrets/private-key.pem'),
  3. cert: fs.readFileSync('./secrets/public-certificate.pem'),
  4. };
  5. const app = await NestFactory.create(ApplicationModule, {
  6. httpsOptions,
  7. });
  8. await app.listen(3000);

If using Fastify create the app like this:

  1. const app = await NestFactory.create<NestFastifyApplication>(
  2. ApplicationModule,
  3. new FastifyAdapter({ https: httpsOptions }),
  4. );

Multiple simultaneous servers

A full control over the library instance gives a simple way to create a several multiple simultaneous servers that are listening on different ports.

  1. const httpsOptions = {
  2. key: fs.readFileSync('./secrets/private-key.pem'),
  3. cert: fs.readFileSync('./secrets/public-certificate.pem'),
  4. };
  5. const server = express();
  6. const app = await NestFactory.create(
  7. ApplicationModule,
  8. new ExpressAdapter(server),
  9. );
  10. await app.init();
  11. http.createServer(server).listen(3000);
  12. https.createServer(httpsOptions, server).listen(443);

info Hint The ExpressAdapter is imported from the @nestjs/platform-express package.